Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

pilou492

macrumors newbie
Original poster
Jul 23, 2009
3
0
Hi

I'm trying to draw a nice outline aroung an NSRect object. To see what I meen, you can check out the selection box on MAC OS X when you want tou grab something on your Desktop. It draw a nice and white rect with a hard white outline arround it. Could you help me doing the same thing ? I've been trying for couple of hours and all I have, is the white rect but nothing arround it...

Thx guys
 
Something like this? (untested)

Code:
[[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] set];
NSRectFill(NSInsetRect(rect, -1.0, -1.0), NSCompositeSourceOver);
NSRectFill(rect, NSCompositeSourceOver);
 
A little more help

Hi, thx for your response.

However, i can't still figure out how to make this thing working ... lol

I try the piece of code above, but at first, there is too much parameters to the function NSRectFill. I change that, but nothing. Let me give you my code :

- (void)drawRect: (NSRect)rect
{
NSRect bounds = [self bounds];
// [[NSColor whiteColor] set];
[[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] set];
NSRectFill(NSInsetRect(rect, 1.0, 1.0));
NSRectFill(rect);
[NSBezierPath fillRect:bounds];
}

That's what I have right now, i'm sure it is very bad, but i'm starting dealing with NSRect things ^^

Thx a lot
 
Hi,

I have done my "outlines" in different way, but anyway I see that you use

Code:
NSRectFill(NSInsetRect(rect, 1.0, 1.0));
NSRectFill(rect);
[NSBezierPath fillRect:bounds];
}

The NSInsetRect(rect, dx, dy) copies the rect with the offsets of dx & dy, In your example it seems that you move it inside (inward) and thus write over what you did by executing next following lines

NSRectFill(rect);
[NSBezierPath fillRect:bounds];

Try to move outwards by using dx and dy initialized to -1.0 It may help.

Use debugger and step between lines, it will help to understand what is going on.
Good luck.
 
If you're going to use a fillRect for the outline, you want to make sure to draw it before the "inner" rectangle, otherwise you'll never see it. Personally, I'd use strokeRect--it's faster and doesn't have this problem. Plus, if you're doing a lot of drawing with it (such as a highlight), you won't get clipping issues.

I just tested this, and it works. Set the colors as you like them.
Code:
- (void)drawRect:(NSRect)rect {
        NSRect outer = NSMakeRect(80, 80, 100, 50);
	NSRect inner = NSInsetRect(outer, 1, 1);
	
	[[NSColor blueColor] set];
	[NSBezierPath fillRect:inner];
	
	[[NSColor whiteColor] set];
	[NSBezierPath strokeRect:outer];
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.