Search code examples
objective-ccocoadrawingnsbezierpath

Cut a rectangle out of an NSBezierPath


Is it possible to remove a chunk of NSBezierPath that is defined by an NSRect region within the path?


Solution

  • Absolutely. This is what clipping regions do:

    // Save the current clipping region
    [NSGraphicsContext saveGraphicsState];
    NSRect dontDrawThisRect = NSMakeRect(x, y, w, h);
    // Either:
    NSRectClip(dontDrawThisRect);
    // Or (usually for more complex shapes):
    //[[NSBezierPath bezierPathWithRect:dontDrawThisRect] addClip];
    [myBezierPath fill];    // or stroke, or whatever you do
    // Restore the clipping region for further drawing
    [NSGraphicsContext restoreGraphicsState];