NSTextField中的setStringValue将始终更改高度

时间:2014-12-16 03:30:30

标签: cocoa constraints newline nstextfield

我在视图中有一个NSTextField,其中布局完全由约束控制,而且translatesAutoresizingMaskIntoConstraints为NO。我尝试使用setStringValue来改变这样的内容:

[[self textfield] setStringValue:@"1\n2\n3\n"];

然后将高度改为4行,这不是我想要的。我需要一个只能显示一行的NSTextField,但我仍然可以使用向上和向下箭头键进入不同的行。就像使用选项+ enter在NSTextField中插入换行符一样。

我也试图保持身高:

NSRect originalFrame = [[self textfield] frame];
[[self textfield] setStringValue:@"1\n2\n3\n"];
NSRect newFrame = [[self textfield] frame];
newFrame.size.height = originalFrame.size.height;
[[self textfield] setFrame:newFrame];

它不起作用。我检查了intrinsicContentSize并返回(width = -1,height = 73)。有什么我可以设置为NSTextField所以高度只有一行像22?

1 个答案:

答案 0 :(得分:1)

这种情况正在发生,因为您已根据视图设置了文本字段的自动布局。因此,只需单击下面的高度复选框即可修复文本字段的高度: - enter image description here

相关问题