我在 View 中有一个 NSTextField,其中布局完全由约束控制,并且 translatesAutoresizingMaskIntoConstraints 为“否”。我尝试使用 setStringValue 来更改内容,如下所示:
[[self textfield] setStringValue:@"1\n2\n3\n"];
然后高度改为4行,这不是我想要的。我需要一个只能显示一行的 NSTextField,但我仍然可以使用向上和向下箭头键进入不同的行。就像使用 option+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,它返回(宽度=-1,高度=73)。我可以为 NSTextField 设置什么,以便高度只有一行,例如 22?
最佳答案
发生这种情况是因为您已经设置了相对于 View 的文本字段的自动布局。因此,只需通过单击高度复选框来固定文本字段的高度,如下所示:-
关于cocoa - NSTextField 中的 setStringValue 总是会改变高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27497076/