Re: NSView drawing with superimposed text
Re: NSView drawing with superimposed text
- Subject: Re: NSView drawing with superimposed text
- From: Erik Buck <email@hidden>
- Date: Sun, 2 Jan 2011 23:24:36 -0500
Each time -drawRect: is called, the rect parameter is a dirty rectangle calculated by the frameworks to be the smallest area that needs to be redraw. With your implementation, you are probably filling several small sub-rectangles of the view's bounds with white and then stroking lines in only one of the small rectangles that the framework thinks need update.
If you want to erase the entire view every time drawRect is called, try either [NSBezierPath fillRect:[self bounds]]; or [NSBezierPath fillRect:[self visibleRect]];
If you want the entire view to redraw for some reason, then send -setNeedsDisplay:YES to the view. If you want to redraw a sub rect of the view, send -setNeedsDisplayInRect:. Don't send either message from within the implementation of -drawRect: because it is too late then. The area that needs to be redrawn has already been calculated and the changes you make via setNeedsDisplay will be replaced/recalculated by the framework after -drawRect: returns. Also, don't call any variant of [self display] within -drawRect: because it may result in infinite recursion.
Finally, if you want to be cleaver and optimize drawing, you can use [self getRectsBeingDrawn:&rects count:&count]; and implement method -wantsDefaultClipping to return NO.
Reference: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/CocoaViewsGuide/SubclassingNSView/SubclassingNSView.html
On Jan 2, 2011, at 1:36 PM, Michael McLaughlin wrote:
> With Xcode 3.2.5 and the corresponding IB, I have a custom NSView into which I want to draw. This view has 15 static text views in front of it, in the contentView only, all of which are filled in before the custom drawing and none of which draw their own backgrounds. The code for the custom drawing is
>
> -(void)drawRect:(NSRect)rect
> {
> [[NSColor blackColor] setStroke];
> [[NSColor whiteColor] setFill];
> [NSBezierPath fillRect:rect];
>
> if (phase3) { // draw line
> NSBezierPath *path = [NSBezierPath bezierPath];
> [path moveToPoint:NSMakePoint(0, 0)];
> [path lineToPoint:NSMakePoint(520, 240)];
> [path stroke];
> }
> }
>
> This method is called three times during a complete rendering.
>
> Without the conditional (if statement), the result is as expected but, with it, the final (third) call to this method references a rect that does not correspond to anything in the window and the final window shows only a small portion of the line even though all of the superimposed text items should be transparent apart from their text.
>
> Obviously, I am missing something important about Cocoa drawing but could find no hints anywhere to help.
>
> Any tips?
>
> TIA.
>
>
>
>
>
> _______________________________________________
>
> Cocoa-dev mailing list (email@hidden)
>
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
>
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden