View Full Version : bounds

Jun 16, 2009, 01:34 AM
I have been trying to use bounds in a custom cell program, as described in a prior post "drawAtPoint". So I wrote a simple customview program, whose code is below...

@implementation AppController

- (id)initWithFrame:(NSRect)frame {
self = [super initWithFrame:frame];
if (self) {
// Initialization code here.
return self;

- (void)drawRect:(NSRect)rect {
NSRect myBounds=[self bounds];
[[NSColor greenColor]set];

NSPoint myPoint1=myBounds.origin;
NSPoint myPoint2=NSMakePoint(50,50);

[[NSColor blackColor]set];
myPath=[NSBezierPath bezierPath];

[myPath setLineWidth:3];
[myPath moveToPoint:myPoint1];
[myPath lineToPoint:myPoint2];
[myPath stroke];


This works, drawing a simple green box with a diagonal black line in the bottom, left corner. However, when using bounds in the bottom code...

-(void)drawWithFrame:(NSRect)cellFrame inView:(NSView*)controlView
NSRect myRect=NSZeroRect;
NSRect myBounds=[self bounds];
NSRect anInsetRect = NSInsetRect(cellFrame,10,10);
NSRect aTextBox = NSMakeRect(anInsetRect.origin.x,anInsetRect.origin.x,25,25);
[[NSColor redColor]set];
[string drawInRect:aTextBox withAttributes:attributes];

I get the error "invalid initializer". Is bounds inappropriate for drawWithFrame:inView:? Any other ideas? Thanks.


Jun 16, 2009, 08:08 AM
Look up the bounds method in the documentation, and you'll see that it doesn't exist for NSCell or any of its subclasses. It's strictly an NSView method, and NSViews do not work the same as NSCells. When drawing with an NSCell you should only be using the cellFrame parameter that gets passed to your code.