• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Unwanted artifacts when using controls in NSView subclass
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Unwanted artifacts when using controls in NSView subclass


  • Subject: Re: Unwanted artifacts when using controls in NSView subclass
  • From: Paul Lynch <email@hidden>
  • Date: Mon, 3 Apr 2006 21:57:25 +0100

What happens if you use [self bounds] instead of rect in your first line?

The Cocoa code sure seems a lot neater, doesn't it?

Paul

On 3 Apr 2006, at 21:45, email@hidden wrote:

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


_______________________________________________ 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
References: 
 >Unwanted artifacts when using controls in NSView subclass (From: email@hidden)

  • Prev by Date: Re: Strange indexOfTabViewItemWithIdentifier behavior
  • Next by Date: Re: Path names in executable
  • Previous by thread: Unwanted artifacts when using controls in NSView subclass
  • Next by thread: Re: Unwanted artifacts when using controls in NSView subclass
  • Index(es):
    • Date
    • Thread