views:

1109

answers:

4

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.

I usually use (and very much like) IB, but in this case it must be done programatically.

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.

Can anyone provide or point out some example code of how to do this programatically? Thx.

A: 

Specifically, you will want to setBordered:NO, and set the bezel style to whatever that bezel style is which I forgot. Also setEditable:NO, and optionally setSelectable:NO. That should suffice.

Steven Degutis
hrm, yeah i've tried that, and it doesn't seem to get me very close to the label behavior seen in IB.
Todd Ditchendorf
Bezel style isn't necessary. You do also want to set `drawsBackground` to NO.
Peter Hosey
+5  A: 

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];
}

This will produce the following: http://dl.getdropbox.com/u/925956/label-test.png

naixn
yeah, i'm really looking for more than this basic information. That does not produce the 'label-y' behavior seen in IB.
Todd Ditchendorf
@Todd: I've edited my post, with a working code :)
naixn
this is pretty much exactly what i've tried. does not produce label-y behavior like in IB :(
Todd Ditchendorf
Really? It does work for me though: http://dl.getdropbox.com/u/925956/label-test.png. Or maybe I do not understand what you mean by "label-y"?
naixn
my bad. i was wrong. i had something else weird that was going wrong. this does indeed get me very close to what i'm looking for. :)
Todd Ditchendorf
The only thing I see missing there is the font is wrong. If you're completely creating the text field and want to use the default system fonts: [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:[[textField cell] controlSize]]]
Ashley Clark
+3  A: 

This can be tricky to get right. I don't have the recipe for an exact replica handy, but when I've been stuck in a similar situation, here's what I do:

  1. Create a UI element in IB.
  2. Add an outlet to it from my controller class.
  3. Break in gdb in awakeFromNib or whatever.
  4. From the gdb prompt, "p *whateverOutlet" ... this will show you the C struct contents of the label NSTextField that IB set up.

By looking at all the myriad values in there, you can get a lot of guesses about what you're neglecting to set. Usually it ends up being some magic combination of bezel and border settings, that gets you where you want to be.

danielpunkass
nice tip! i'll try that :)
Todd Ditchendorf
+2  A: 

You could try using nib2objc to get all the properties that IB sets

g-Off