How to create outline aroung NSRect

Discussion in 'Mac Programming' started by pilou492, Aug 13, 2009.

  1. pilou492 macrumors newbie

    Joined:
    Jul 23, 2009
    #1
    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
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Something like this? (untested)

    Code:
    [[NSColor colorWithCalibratedWhite:1.0 alpha:0.5] set];
    NSRectFill(NSInsetRect(rect, -1.0, -1.0), NSCompositeSourceOver);
    NSRectFill(rect, NSCompositeSourceOver);
    
     
  3. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
  4. pilou492 thread starter macrumors newbie

    Joined:
    Jul 23, 2009
    #4
    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
     
  5. petron macrumors member

    Joined:
    May 22, 2009
    Location:
    Malmo, Sweden
    #5
    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.
     
  6. Cinder6 macrumors 6502

    Cinder6

    Joined:
    Jul 9, 2009
    #6
    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];
    }
     

Share This Page