Mar
16
Day 18: Custom drawing (6) : -strokeInside
Using the articles from the past few days, we now have all the ingredients to (finally) make an Inner Stroke. Because we will use -addCllip: we have to save and restore the graphics context, otherwise any subsequent drawing after this will have the same clipping path set.
@implementation NSBezierPath (365Cocoa)
- (void)strokeInside
{
[NSGraphicsContext saveGraphicsState];
CGFloat oldWidth = [self lineWidth];
[self setLineWidth:oldWidth*2];
[self addClip];
[self stroke];
[self setLineWidth:oldWidth];
[NSGraphicsContext restoreGraphicsState];
}
The only thing left to explain here is why I bother with setting line widths. The reason is that when setting NSBezierPath’s lineWidth, the stroke width expands in both directions. With -addCllip:, we’ll cut out half the stroke that falls outside our border so to compensate we have to double the size of the stroke. To make sure our method doesn’t have any undocumented side-effects, we restore the line width to what it was before.