Day 15: Custom drawing (4) : NSBezierPath
Yesterday we’ve seen a tiny bit of drawing code but for real drawing code you’ll most likely be using NSBezierPath or images. Today, a short introduction on the former. NSBezierPath draws bezier paths (stating the obvious), which are either straight lines or curves. These segments can be mixed at will creating almost any shape imaginable. Apart from that, NSBezierPath offers some simple class methods for creating rectangles, ovals and rounded rectangles. We’ll use those today.
Inside our -drawRect: we create an NSBezierPath, set a color, fill it, set another color and then stroke it. This is important; setting a color has the effect that most of the drawing done after this will be done in this color. Some drawing functions take their own colors but many will use the current color. Of course, setting new colors will replace the previous color.
- (void)drawRect:(NSRect)dirtyRect
{
NSRect rect = NSMakeRect(10, 10, 100, 100);
NSBezierPath *oval = [NSBezierPath bezierPathWithOvalInRect:rect];
[[NSColor redColor] set];
[oval fill];
[[NSColor blueColor] set];
[oval setLineWidth:4];
[oval stroke];
}