Unwanted artifacts when using controls in NSView subclass
Unwanted artifacts when using controls in NSView subclass
- Subject: Unwanted artifacts when using controls in NSView subclass
- From: email@hidden
- Date: Mon, 03 Apr 2006 20:45:29 +0000
Hi all,
I'm playing with subclassing NSView to allow for some colored backgrounds and borders around sections of my window. I'm overriding the drawRect just as in most examples.
However, I have a problem that if I place a button in my custom view, then click the button in my running application my stroke and fill color encompass the button afterwards.
Here's the snippet I use to override my drawRect...
- (void)drawRect:(NSRect)rect {
rect = NSInsetRect(rect,2.0,2.0);
NSColor *bgColor = [NSColor colorWithDeviceRed:.5 green:0.5 blue:0.5 alpha:0.5];
NSColor *strokeColor = [NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0];
CGRect cgRect = CGRectMake(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height);
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort];
CGContextSaveGState(context);
CGContextSetFillColorWithColor(context,[bgColor convertToCGColorRef]); //convertToCGColorRef is contained within a category for NSColor.
CGContextSetStrokeColorWithColor(context,[strokeColor convertToCGColorRef]);
CGContextSetLineWidth(context,2.0);
CGContextFillRect(context,cgRect);
CGContextStrokeRect(context,cgRect);
CGContextRestoreGState(context);
}
I've also tried the Cocoa drawing way, without going to Quartz engine directly...
- (void)drawRect:(NSRect)rect {
rect = NSInsetRect(rect,2.0,2.0);
NSColor *bgColor = [NSColor colorWithDeviceRed:.5 green:0.5 blue:0.5 alpha:0.5];
NSColor *strokeColor = [NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:1.0];
NSBezierPath *path = [NSBezierPath bezierPathWithRect:rect];
[path setLineWidth:2.0];
[bgColor setFill];
[strokeColor setStroke];
[path fill];
[path stroke];
}
Thanks in advance for any assistance.
- Paul
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden