将NSTextField转换为Label

时间:2013-07-02 09:30:05

标签: objective-c macos cocoa nstextfield nstextfieldcell

我使用以下功能将文本字段转换为标签,反之亦然

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

}  

但我的用户界面不一致。

  • 初始标签

enter image description here

  • 标签到文字字段

enter image description here

  • Textfield to Label(选择文字后)

enter image description here

  • 标签到文字字段

enter image description here

enter image description here

任何人都可以帮助我。

2 个答案:

答案 0 :(得分:3)

Culprit是标签的默认高度。

标签默认高度

enter image description here

NSTextField默认高度

hight of textfield

下面是标签的代码,高度为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];

答案 1 :(得分:0)

尝试设置调整大小掩码

[textField setAutoresizingMask: NSViewMinXMargin | NSViewWidthSizable | NSViewMaxXMargin | NSViewMinYMargin | NSViewHeightSizable | NSViewMaxYMargin];

See this answer