我有两个名为headingLabel和descriptionLabel的UILabel,其行数分别为3和5。现在我希望文本的字体大小能够根据收到的一些网络json数据进行更改并适应文本变化的numberOfLines。
here is my code
let headingLabel : UILabel = {
let hl = UILabel()
hl.translatesAutoresizingMaskIntoConstraints = false
hl.text = "Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor"
hl.textColor = .black
hl.lineBreakMode = NSLineBreakMode.byWordWrapping
hl.numberOfLines = 3;
hl.sizeToFit()
hl.clipsToBounds = true
hl.font = hl.font.withSize(20)
return hl
}()
let descriptionLabel : UILabel = {
let dl = UILabel()
dl.translatesAutoresizingMaskIntoConstraints = false
dl.text = "Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolorLorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor Lorem ipsum dolor sit amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor sit amet amet Lorem ipsum dolor"
dl.textColor = .black
dl.lineBreakMode = NSLineBreakMode.byWordWrapping
dl.numberOfLines = 5
dl.font = dl.font.withSize(18)
dl.sizeToFit()
dl.clipsToBounds = true
dl.alpha = 0.8
return dl
}()
答案 0 :(得分:1)
使用自动收缩功能将文本调整为标签框。
确定标签是否在进行截断之前调整文本的外观。选择“最小字体比例”并输入一个值,以允许标签缩小字体大小以适合文本。启用“紧固字母间距”以允许标签减少字符间距。在运行时使用minimumScaleFactor和allowDefaultTighteningForTruncation属性分别访问这些值。
答案 1 :(得分:0)