Search code examples
objective-ccocoansviewnsbox

Drawing Line Separator with Cocoa


How would one draw a line separator such as this:

enter image description here

Note that there's 2 single pixel lines on top of each other. Any tips?


Edit:

Here's the code I needed, in a NSBox subclass: (or NSView, doesn't really matter):

- (void)drawRect:(NSRect)rect
{
    [[NSColor lightGrayColor] set];
    NSRectFill(NSMakeRect(0, 1, NSWidth(rect), 1));
    
    [[NSColor whiteColor] set];
    NSRectFill(NSMakeRect(0, 0, NSWidth(rect), 1));    
}

Solution

  • Typically this kind of separator is drawn with an NSBox, usually configured as an NSBoxSeparator type. That's not quite the look you're looking for, though. I'd recommend hand-drawing it in an NSBox subclass (so you get the normal NSBox behaviors). For an example of an NSBox subclass, see Matt Gemmell's RoundedBox.

    You just need two lines, so the drawRect: should be very simple. Encapsulating it this way will make it extremely flexible for you.

    (Of course you can also consider just using the standard NSBox separator rather than creating a custom look. That's what it's there for.)