【发布时间】:2017-07-07 09:14:34
【问题描述】:
我想在结尾处创建带有 Read More/Read Less 的段落。
这是我的代码;
func getLabelHeight(text: String, width: CGFloat, font: UIFont) -> CGFloat {
let lbl = UILabel(frame: .zero)
lbl.frame.size.width = width
lbl.font = font
lbl.numberOfLines = 0
lbl.text = text
lbl.sizeToFit()
lbl.adjustsFontSizeToFitWidth = true
return lbl.frame.size.height
}
@IBAction func btnReadMore(_ sender: Any) {
if isLabelAtMaxHeight {
btnReadmore.setTitle(NSLocalizedString("Read more", comment: ""), for: .normal)
btnReadmore.titleLabel!.font = UIFont (name: "Tharlon", size: 13)
isLabelAtMaxHeight = false
lblReviewHeight.constant = 29
lblReview.font = UIFont (name: "Tharlon", size: 13)
}
else {
btnReadmore.setTitle(NSLocalizedString("Read less", comment: ""), for: .normal)
btnReadmore.titleLabel!.font = UIFont (name: "Tharlon", size: 13)
isLabelAtMaxHeight = true
lblReviewHeight.constant = getLabelHeight(text: lblReview.text!, width: view.bounds.width, font: lblReview.font)
lblReview.font = UIFont (name: "Tharlon", size: 13)
lblReview.lineBreakMode = NSLineBreakMode.byTruncatingHead
}
}
我还在属性检查器中设置了标签“自动换行”。
问题是当我在 Read Less 部分添加 "NSLineBreakMode.byTruncatingHead" 时,所有文本都完全显示。但是,文本中那些地方的一些单词消失了。
因此,我删除了该代码并运行该应用程序。那时,文本没有完全显示,只显示一半。我一整天都在努力解决这个问题。
我不想使用任何其他库。
谁能帮帮我?
【问题讨论】:
标签: ios swift3 uilabel nsattributedstring