Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

medasmx

macrumors member
Original poster
Nov 9, 2008
89
0
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...

Code:
@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];
	NSRectFill(myBounds);
	
	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];
}

@end

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...

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];
	NSRectFill(NSInsetRect(cellFrame,4,4));
	[string drawInRect:aTextBox withAttributes:attributes];
}

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

Adam
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.