Create a gist now

Instantly share code, notes, and snippets.

BNRBlockView: Add custom-drawn views to the Cocoa view hierarchy without subclassing.
@class BNRBlockView;
typedef void(^BNRBlockViewDrawer)(BNRBlockView *view, NSRect dirtyRect);
@interface BNRBlockView : NSView {
BNRBlockViewDrawer drawBlock;
BOOL opaque;
}
+ (BNRBlockView *)viewWithFrame:(NSRect)frame
opaque:(BOOL)opaque
drawnUsingBlock:(BNRBlockViewDrawer)drawBlock;
@property (nonatomic, copy) BNRBlockViewDrawer drawBlock;
@property (nonatomic, assign) BOOL opaque;
@end
#import "BNRBlockView.h"
@implementation BNRBlockView
@synthesize drawBlock, opaque;
+ (BNRBlockView *)viewWithFrame:(NSRect)frame
opaque:(BOOL)opaque
drawnUsingBlock:(BNRBlockViewDrawer)theDrawBlock
{
__typeof__(self) view = [[BNRBlockView alloc] initWithFrame:frame];
[view setDrawBlock:theDrawBlock];
[view setOpaque:opaque];
return [view autorelease];
}
- (void)dealloc
{
[drawBlock release];
[super dealloc];
}
- (void)drawRect:(NSRect)dirtyRect {
if (drawBlock)
drawBlock(self, dirtyRect);
}
- (BOOL)isOpaque {
return opaque;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment