Home > other >  How to display NSText to NSWindow?
How to display NSText to NSWindow?

Time:08-03

I have the following code:

#include <Cocoa/Cocoa.h>

NSColor *createColor(float r, float g, float b, float a) {
    return [NSColor colorWithCalibratedRed:r green:g blue:b alpha:a];
}

NSRect createRect(float startX, float startY, float width, float height) {
    return NSMakeRect(startX, startY, width, height);
}

int main() { @autoreleasepool {
        NSWindow *window = [[NSWindow alloc] init];
        window.title = @"Title";
        window.subtitle = @"Subtitle";

        NSText *label = [[NSText alloc] initWithFrame: createRect(10, 10, 20, 20)];
        t.string = @"test";

        [window setFrame:createRect(0, 0, 300, 300) display:YES animate:YES];
        [window setBackgroundColor: createColor(0.5, 1, 0.1, 1)];
        [window makeKeyAndOrderFront:nil];
        while (1) {
            NSEvent *event = [window nextEventMatchingMask:NSEventMaskAny];
        }
    }
}

Is there an simple way to display to NSWindow such as:

[window *command*: label];

I have looked at this post but it does not address my issue as it uses NSApplication rather than NSWindow. Also, I would prefer to use NSText instead of NSTextField.

CodePudding user response:

Its as easy as:

[window.contentView addSubview: label];
  • Related