简体   繁体   English

为什么将useSingleLineMode设置为YES的NSTextField具有多行文本的固有内容大小?

[英]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: 当然,我可以:

  • Overload intrinsicContentSize method 重载nativeContentSize方法
  • Filter strings keeping only first line 过滤字符串仅保留第一行

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.

相关问题 iOS:如果我将UILabel添加到UIView,则视图是否具有标签的固有内容大小? - iOS: if I add UILabel to a UIView, will the view has intrinsic content size of the label? 在NSTextField中设置文本垂直中心 - Set text vertical center in NSTextField 根据字符串值调整NSTextField内容大小 - Adjust NSTextField content size based on the string value 如何在NSTextField中查找文本占用的大小? - How to find the size occupied by the text in NSTextField? 如何设置NSWindow的大小,相对NSTextField的高度? - How set size NSWindow, relative NSTextField height? 如何用视觉语言格式编写内容大小优先级的内容? - How to write content hugging priority of intrinsic size in visual language format? 如何在UILabel的内在内容大小中添加填充? - How can I add padding to the intrinsic content size of UILabel? 根据内容长度修改 NSTextField 的字体大小 - Modifying an NSTextField's font size according to content length Cocoa:如何制作多行NSTextField? - Cocoa : How to make multiline NSTextField? 不能在 facebook ios ZEAE18BC41E1434DD98FA2DD98Z93 的 ZEAE18BC41E1434DD98FA2DD98Z93 后台运行时 ZEAE18BC41E1434DD98FA2DD98Z93 - Can not post on facebook wall with facebook ios sdk when Application does not run in background has set to YES
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM