[英]Why does NSTextField with usesSingleLineMode set to YES has intrinsic content size of multiline text?
I want to draw single line textfield with default border. 我想用默认边框绘制单行文本字段。
So I entered some multiline text into it(just for test for future cases) and set the flags 所以我在其中输入了多行文本(仅用于将来的测试)并设置了标志
Swift: 迅速:
textfield.usesSingleLineMode = true
textfield.maximumNumberOfLines = 1
Objective-C: Objective-C的:
self.textfield.usesSingleLineMode = YES;
self.textfield.maximumNumberOfLines = 1;
But I got in console: 但是我进入了控制台:
(lldb) po self.textfield.intrinsicContentSize
(width = 511.5, height = 174)
I've tested all lineBreakMode s, including NSLineBreakByTruncatingTail . 我已经测试了所有lineBreakMode ,包括NSLineBreakByTruncatingTail 。
Here is a field in IB: 这是IB中的一个字段:
Of course I can: 当然,我可以:
But it looks like a crutch =( Do you have any ideas how to fix it without crutches? Do you think it is an AppKit bug or my mistake? 但是,它看起来像拐杖=(您是否有任何想法而不用拐杖去修理它?您认为这是AppKit的错误还是我的错误?
PS In any case, thanks for attention. PS无论如何,感谢您的关注。
This is not a bug. 这不是错误。 By default the NSTextField is trying to display as much text possible.
默认情况下,NSTextField尝试显示尽可能多的文本。 I made a sample application, that contains a long line of text in the title of the NSTextField.
我制作了一个示例应用程序,该应用程序在NSTextField的标题中包含一长行文本。 Set it to "Uses Single Line Mode" and set line break to "Truncate Tail".
将其设置为“使用单行模式”,并将换行符设置为“截尾”。 I put leading, top and trailing layout constraints on the view.
我在视图上放置了开头,顶部和结尾的布局约束。 Now when i input the text into the text field, it will make the horizontal width of the window very wide.
现在,当我在文本字段中输入文本时,它将使窗口的水平宽度非常宽。 This is Autolayout in action.
这是自动布局。
To fix this you need to select the NSTextField, choose the Size inspector and set the Horizontal Content Compression Resistance Priority to a lower number. 要解决此问题,您需要选择NSTextField,选择“大小”检查器,然后将“水平内容压缩优先级”设置为一个较小的数字。 In the sample application I changed it from 750 to low (250).
在示例应用程序中,我将其从750更改为低(250)。
Resize content view to an acceptable size, and you are off to the races. 将内容视图调整为可接受的大小,就可以开始比赛了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.