Skip navigation.
 
mlRe: NSTextView subclass redrawing weirdness
FROM : Antonio Nunes
DATE : Sun Jul 09 09:46:25 2006

On 9 Jul 2006, at 02:07, John Cassington wrote:

> I have created a subclass of on NSTextView so that I can add a 1 pixel
> border around the edge of it. This is working, but it seems to be 
> getting
> darker every time i do something that causes a redraw (such as hitting
> enter).

[...]
> -(void)drawRect:(NSRect)rect {
>    NSRect wholeRect = [self bounds];
>    NSRect textRect = NSMakeRect(wholeRect.origin.x
> +1,wholeRect.origin.y+1,
> wholeRect.size.width-2,wholeRect.size.height-2);
>
>    [[NSColor blackColor] set];
>    [NSBezierPath setDefaultLineWidth:1];
>    [NSBezierPath strokeRect:wholeRect];
>
>    [super drawRect:textRect];
> }


Hi John,

The progressive darkening is the result of accumulated anti-aliased 
drawing. The first time the frame is drawn it isn't the color you 
specified anyway because of anti-aliasing. Did you notice that? 
Unless you want gray in which case that is the color you should 
specify. So you should clear the rect before drawing it again (see 
NSEraseRect). But you can get the result you want also without using 
an NSBezierPath. Try this:

-(void)drawRect:(NSRect)rect {
   NSRect wholeRect = [self bounds];
   NSRect textRect = NSInsetRect(wholeRect,1,1);
   
   [[NSColor blackColor] set];  // or grayColor if that's what you want
   NSFrameRect(wholeRect);
   
   [super drawRect:textRect];
}

Looks OK to me.

Cheers,
António Nunes

-----------------------------------------
Accepting others as they are
brings a wonderful freedom
to your own mind.

--The Peace Formula
-----------------------------------------

Related mailsAuthorDate
mlNSTextView subclass redrawing weirdness John Cassington Jul 9, 03:07
mlRe: NSTextView subclass redrawing weirdness Antonio Nunes Jul 9, 09:46