Core Graphics Basics(NS)

Core Graphics Basic on Mac Cocoa.

Cocoa version is a bit different from iOS version. iOS uses UIxxx.
In Cocoa, we use NSxxx.

Requirement

We need CoreGraphics.framework to use CGXxxx, CoreGraphics API.

Let’s make custom NSView first.

Custom NSView

When creating NSView in XCode, some codes has already written in .h, .m

Header

@interface CustomView : NSView
@end

Code

@implementation CustomView

- (id)initWithFrame:(NSRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

- (void)drawRect:(NSRect)dirtyRect
{
    NSColor *backColor = [NSColor colorWithCalibratedRed:0.333 green:0.333 blue:0.333 alpha:0.3f];
    [backColor set];
    [NSBezierPath fillRect:dirtyRect];
}

This is example to fill background(gray color).
drawRect is keypart. We add code to draw something here.

This view can be used in WindowController or other places to use addSubView or loadView.