Sample code for creating a NSTextField “label”?

前端 未结 6 1072
灰色年华
灰色年华 2020-12-24 01:53

In my desktop Mac OS X app, I\'d like to programatically create a NSTextField \"label\" which has the same behavior and properties as a typical label created in Interface Bu

6条回答
  •  無奈伤痛
    2020-12-24 02:21

    A label is actually an instance of NSTextField, a subclass of NSView. So, since it is a NSView, it has to be added to another view.

    Here's a working code:

    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        NSTextField *textField;
    
        textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 200, 17)];
        [textField setStringValue:@"My Label"];
        [textField setBezeled:NO];
        [textField setDrawsBackground:NO];
        [textField setEditable:NO];
        [textField setSelectable:NO];
        [view addSubview:textField];
    }
    

提交回复
热议问题