自动调整 UILabel 大小
有没有办法自动调整 UILabel 的大小?给定大小 40 x 40,文本字体大小将根据字符数进行调整。
Is there a way to auto size a UILabel? given size 40 x 40 the text font size would adjust based on the number of characters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
adjustFontSizeToFitWidth
属性。所以像这样的事情。在 Interface Builder 中,标签属性屏幕上有一个复选框,允许您调整字体大小以适合标签。
You can use the
adjustFontSizeToFitWidth
property. So something like this.In Interface Builder there is a check box on the Label Attributes screen to allow you to adjust the font size to fit the label as well.
嗯,你检查过 UILabel API http ://developer.apple.com/iphone/library/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html 有一个名为
adjustsFontSizeToFitWidth
的简洁属性uhm, did you check out the UILabel API http://developer.apple.com/iphone/library/documentation/uikit/reference/UILabel_Class/Reference/UILabel.html there's a neat property called
adjustsFontSizeToFitWidth
采用autolayout设计理念,不为UILabel设置高度限制,设置no。行数为 0。
自动布局根据标签文本自动处理标签的动态高度。如果标签具有单行文本,则它将仅占用单行空间。如果标签有多于一行,那么它将根据文本大小和显示文本所需的行数调整标签大小。
将动态文本信息的行数设置为零,当文本变化时这将很有用。
以编程方式 (Swift 4)
使用 Inetrface Builder
注意:不需要设置
最小字体比例
,但最好有一个最小比例因子以使文本适合标签框架。参考: UILabel - numberOfLines
With autolayout design concept, do not set height constraints for UILabel and set no. of lines as 0.
Autolayout automatically take care of dynamic height of label according to text of label. If label has single line text then it will occupy single line space only. And if label has more than one line then it will resize label according to text size and number of lines required to display text.
Set number of lines zero for dynamic text information, it will be useful when your text are varying.
Programatically (Swift 4)
Using Inetrface Builder
Note: It is not required to set
Minimum Font Scale
, but nice to have a minimum scale factor to fit text into label frame.Ref: UILabel - numberOfLines
最好使用内在内容和抗压缩优先级来调整标签内容的大小。
Its better to use Intrinsic content and compression resistance priorities to adjust the size of label w.r.t content.