Discussion in 'Mac Programming' started by medasmx, Jun 15, 2009.

  1. medasmx macrumors member

    Nov 9, 2008
    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.

  2. kainjow Moderator emeritus


    Jun 15, 2000
    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.

Share This Page