Design

Custom Drawing Using drawRect, Part 1

One of the more advanced techniques for creating custom user interfaces on the Mac is the use of NSView’s drawRect method.  Many answers to questions on StackOverflow and Apple’s mailing lists include recommendations to “just override drawRect and do the drawing yourself”.  Some folks see this recommendation and their eyes glaze over, thinking that it’s too advanced of a technique for them to wrap their heads around.  Over the next few days I’m going to go over some basic techniques that can yield powerful results.

Let’s start by setting up the Xcode project that will be the basis of the rest of these posts.

  • Open Xcode and create a new Cocoa Application project called DrawingSample.
  • Create a new NSView subclass called CustomDrawingView.
  • Open MainMenu.xib, add a new Custom View to the Main Window, set its class to be CustomDrawingView, and set it’s autosizing flags as seen here:

DrawingSample IB Layout

Save and Quit Interface Builder and switch back to Xcode.  Open CustomDrawingView.m, it should look like so:

@implementation CustomDrawingView
 
- (id)initWithFrame:(NSRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    return self;
}
 
- (void)drawRect:(NSRect)dirtyRect {
    // Drawing code here.
}
 
@end

We’re going to start (and finish) today with just a simple concept and some basic drawing that will set the stage for the future posts. All drawing in Cocoa is done by first setting up the environment in which you want to draw, and then doing the actual drawing. For instance, if we want to draw a blue box, we first have to setup the color blue, define the bounds of the box, and then draw it.  In this case we are using the NSRect that is passed to the drawRect method as the box we want to draw, and we setup the color blue by calling [[NSColor blueColor] set].  We then use the convenience method NSRectFill to fill the dirtyRect with the color blue.  Notice that we didn’t pass the color to NSRectFill, we set it, and from then on anything we draw will be blue until we change the color.

You can think of drawing in Cocoa much the same way as you would think of painting with a brush.  You dip your brush in a certain paint color, paint the shape you want to paint, and then dip your brush in a new color and paint some more.

- (void)drawRect:(NSRect)dirtyRect {
	[[NSColor blueColor] set];
	NSRectFill(dirtyRect);
}

The preceding code, when run, will generate a view that looks like this:

NSRectFillNow, this may not look like much, but in future posts we will build on these concepts and, hopefully, by the end have drawn some pretty cool and useful things.

The Best Interface Builder Layout Ever

This morning I posted a screenshot of my Interface Builder layout on Twitter.  I didn’t think much of it at the time, but I received enough positive feedback on it that I decided to post it here for generations of future Cocoa developers to find.  The layout gets harder to work with the smaller your screen gets, but it works very well on my 24″ display.  Another tip for working well with IB: keep it in it’s own space and don’t let other apps invade that space.  I find that when I can concentrate just on my IB windows without having to mentally block out background windows it makes my workflow much smoother.

IB Layout

The $300 Million Button

I am continually amazed how some of the most basic design changes can have profound results.

It’s hard to imagine a form that could be simpler: two fields, two buttons, and one link. Yet, it turns out this form was preventing customers from purchasing products from a major e-commerce site, to the tune of $300,000,000 a year. What was even worse: the designers of the site had no clue there was even a problem.

I was blown away by the revelation that of the 160,000 people that were requesting lost passwords every day only 25% of those people ever came back to complete their order.

The $300 Million Button .