Search code examples
objective-cmacoscocoanstextfieldnstextfieldcell

convert NSTextField to Label


I am using following functions for converting textfield to label and vice versa

-(void)convertLabelToTextField : (NSTextField *)Inlabel
{
    [Inlabel setBezeled:YES];
    [Inlabel setDrawsBackground:YES];
    [Inlabel setEditable:YES];
    [Inlabel setSelectable:YES];
}

-(void)convertTextFieldToLable : (NSTextField *)textField
{

    [textField setDrawsBackground:NO];
    [textField setEditable:NO];
    [textField setSelectable:NO];
    [textField setBezeled:NO];

}  

But my UI is not consistent.

  • Initial Label

enter image description here

  • Label to textfield

enter image description here

  • Textfield to Label (after selecting text)

enter image description here

  • Label to textfield

enter image description here

enter image description here

Can anyone please help me out.


Solution

  • Culprit was default hight of label.

    label Default height

    enter image description here

    NSTextField Default height

    hight of textfield

    below is code for Label with height 22

    NSTextField *textField;
    
    textField = [[NSTextField alloc] initWithFrame:NSMakeRect(10, 10, 100, 22)];
    [textField setStringValue:@"My Label"];
    [textField setBezeled:NO];
    [textField setDrawsBackground:NO];
    [textField setEditable:NO];
    [textField setSelectable:NO];