• 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: Tue, 4 Apr 2006 15:12:19 +0100

The online docs have been (relatively) recently updated, and are really quite good:

http://developer.apple.com/documentation/Cocoa/Conceptual/ CocoaDrawingGuide/index.html

The potted explanation is that drawRect: can and will be called with any old rect at all; the rect is NOT the bounds of your view. Bounds just gives you the edges of your possible drawing areas - so, when you want to put a border around, that's what you need to refer to.

For more advanced work, you can choose to draw only the parts of your view that would be visible within the rect passed in. Or even the subset of that rect that is the true requested drawing area - but read the docs for that, it isn't relevant to your simple view.

John's earlier comment about subviews could also be very relevant; the interactions can be a little surprising at times. See the docs again, and look at the AppKit examples on your disk.

Paul

On 4 Apr 2006, at 13:54, email@hidden wrote:

Thanks Paul.

It kind of suprises me that using [self bounds] really makes a difference.

Can you (or anyone else) explain or point to references that would explain why?

- Paul Heal

 -------------- Original message ----------------------
From: Paul Lynch <email@hidden>
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.
_______________________________________________
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: 
 >Re: Unwanted artifacts when using controls in NSView subclass (From: email@hidden)

  • Prev by Date: Re: Path names in executable
  • Next by Date: Adding Frameworks, etc
  • Previous by thread: Re: Unwanted artifacts when using controls in NSView subclass
  • Next by thread: String encodings
  • Index(es):
    • Date
    • Thread