有没有办法自动调整UILabel的大小?给定大小为40 x 40时,文本字体大小将根据字符数进行调整。
答案 0 :(得分:25)
您可以使用adjustFontSizeToFitWidth
属性。就像这样。
UILabel *myLabel = [[UILabel alloc] init];
[myLabel setAdjustsFontSizeToFitWidth:YES];
在Interface Builder中,“标签属性”屏幕上有一个复选框,允许您调整字体大小以适合标签。
答案 1 :(得分:0)
adjustsFontSizeToFitWidth
的简洁属性
答案 2 :(得分:0)
使用自动布局设计概念,不要为UILabel设置高度限制并设置否。行为0 。
Autolayout根据标签文本自动处理标签的动态高度。如果label有单行文本,那么它将仅占用单行空间。如果标签有多行,那么它将根据文本大小和显示文本所需的行数来调整标签大小。
为动态文本信息设置零行数,当文本变化时,它将非常有用。
以编程方式(Swift 4)
var label = UILabel()
let stringValue = "iOS\nmultiline\nlabel\nin\nInterface\nbuilder"
label.text = stringValue
label.numberOfLines = 0 // Set 0, if number of lines not specified.
label.lineBreakMode = .byTruncatingTail // or .byWrappingWord
label.minimumScaleFactor = 0.8 . // It is not required but nice to have a minimum scale factor to fit text into label frame
使用Inetrface Builder
注意:不需要设置Minimum Font Scale
,但最好使用最小比例因子将文本放入标签框中。
答案 3 :(得分:0)
最好使用固有内容和抗压缩优先级来调整标签内容的大小。