Skip to content

Instantly share code, notes, and snippets.

@preble
Created June 29, 2010 00:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save preble/456577 to your computer and use it in GitHub Desktop.
Save preble/456577 to your computer and use it in GitHub Desktop.
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