Nisba
Nisba

Reputation: 3448

macOS draw a rectangle with two holes inside

I have one big CGRect and two small CGRect inside. I want to draw the big CGRect in red and to form two transparent holes corresponding to the small CGRect.

I am not able to do it. I have tried to use NSBezierPath but in macOS there is no method NSBezierPath.CGPath like in UIBezierPath for iOS.

Upvotes: 1

Views: 697

Answers (1)

Rob
Rob

Reputation: 438072

You don't have to use Core Graphics. You can create a NSView subclass and just stroke/fill the path in draw(_:). In Swift 3:

class HolyView: NSView {

    override func draw(_ dirtyRect: NSRect) {
        super.draw(dirtyRect)

        let path = ... // build the `NSBezierPath` however you want

        NSColor.blue.setFill()
        path.fill()
    }

}

You can then add that view programmatically, or you can make it @IBDesignable and add it directly on your storyboard.

enter image description here

Upvotes: 1

Related Questions