Foundation Functions & AppKit Functions Rule

When you are learning the ins and outs of drawRect: Foundation Functions & AppKit Functions Rule.
Unfortunately, it isn’t as easy as the plethora of iOS code out there to learn from. It’s a little different. AppKit will give you artifacts on screen if you don’t feed it a steady diet of fast functions in drawRect:

Here is one, for drawing borders in a rect.

Keep in mind Quartz (Core Graphics) and it’s Objective-C wrappers in AppKit do give you the options to draw strokes (lines) inside or outside of a path, rather they only draw ON the path. That means the size of a stroke in Quartz is half the size in both directions away from the path.

So what’s a poor guy or gal to do? Do what everyone actually does! You draw an inset rect fill inside another inset rect fill. Well, poorly documented AppKit Function to the rescue! Somebody at Apple already did the hard work. You just need to know what parameters to use.

Used to be easy and clear but this function will kick you in the butt with some obscure ARC gotchas.

First gotcha is that the Foundation Functions & AppKit Functions are all C. That mans C arrays do happen and that means you do weird stuff with Objects! Actually not weird in C, just feeding functions a diet of Object pointers.

NSDrawColorTiledRects
(
NSRect boundsRect,
NSRect clipRect,
const NSRectEdge *sides,
NSColor **colors,
NSInteger count
);

sides is a C array of “NSRectEdge” things. What are they? Where are they? This is buried in the Foundation reference docs. But Xcode will tell you, they’re in Geometry.h
Basically, they’re just enum values that mean one of the sides.
NSMinXEdge is one this means the left edge of a rect. (be careful it is not the same as NSMinX in C terms)

You’ll want something like this:

NSRectEdge sides[] = {NSMaxYEdge, NSMinYEdge};

It’s the colors parameter. It’s a double doozy. C array of Objective-C objects. Yay.
It’s actually not bad to set up, it’s making Xcode shut up afterward…

NSColor * darkGrayColor = [NSColor darkGrayColor];
__autoreleasing NSColor *colors[] = { darkGrayColor, darkGrayColor};
NSDrawColorTiledRects( _bounds, _frame, sides, colors, 2 );

See that __autoreleasing in there? You won’t find examples of it easily on the inter webs yet. But you won’t compile on Xcode 4 with ARC without that. Now go draw your lines fastly!