简体   繁体   中英

How to display NSText to NSWindow?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM