用于创建NSTextField“label”的示例代码? [英] Sample code for creating a NSTextField "label"?
问题描述
在我的桌面Mac OS X应用程序,我想编程创建一个NSTextField标签,它具有与Interface Builder中创建的典型标签相同的行为和属性。
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 Builder.
我通常使用(非常喜欢)IB,但在这种情况下,必须以程序化的方式完成。
I usually use (and very much like) IB, but in this case it must be done programatically.
可能,我似乎找不到方法调用的组合,将编程产生与从IB视图库调色板拖动的标签相同的label-y行为。
Try as I might, I can't seem to find the combination of method calls that will programatically produce the same label-y behavior as a "Label" dragged from the IB View Library palette.
任何人都可以提供或指出一些如何做这个程序的示例代码? Thx。
Can anyone provide or point out some example code of how to do this programatically? Thx.
推荐答案
标签实际上是 NSTextField ,NSView的子类。因此,由于它是一个NSView,它必须添加到另一个视图。
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.
这是一个工作代码:
- (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];
}
这篇关于用于创建NSTextField“label”的示例代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!