如何在 Read More/Less with Swift 3 中设置自动调整的标签高度?
How to set label height for auto adjust in Read More/Less with Swift 3?
我想创建段落末尾有 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
}
}
我还在 Attributes Inspector 中设置了标签 "Word wrap"。
问题是,当我在少读部分添加 "NSLineBreakMode.byTruncatingHead" 时,所有文本都显示完整。但是,文本中那些地方的一些词消失了。
所以,我删除了该代码和 运行 应用程序。那时,文字显示不全,只显示一半。我一整天都在努力解决这个问题。
我不想使用任何其他库。
有人可以帮帮我吗?
要按字体和宽度计算文本高度,您可以使用此扩展程序
extension UIFont {
func sizeOfString (_ string: String, constrainedToWidth width: CGFloat) -> CGSize {
return NSString(string: string).boundingRect(with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}
那就叫它
let width = 290.0
let textSize = UIFont.systemFont(ofSize: 15).sizeOfString("text", constrainedToWidth: width)
删除约束 lblReviewHeight
,然后尝试使用 numberOfLines
控制您的文本布局,如果您想要显示所有描述,请设置 numberOfLines = 0
,否则设置 numberOfLines
你想要的行。
我想创建段落末尾有 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
}
}
我还在 Attributes Inspector 中设置了标签 "Word wrap"。
问题是,当我在少读部分添加 "NSLineBreakMode.byTruncatingHead" 时,所有文本都显示完整。但是,文本中那些地方的一些词消失了。
所以,我删除了该代码和 运行 应用程序。那时,文字显示不全,只显示一半。我一整天都在努力解决这个问题。
我不想使用任何其他库。
有人可以帮帮我吗?
要按字体和宽度计算文本高度,您可以使用此扩展程序
extension UIFont {
func sizeOfString (_ string: String, constrainedToWidth width: CGFloat) -> CGSize {
return NSString(string: string).boundingRect(with: CGSize(width: width, height: CGFloat.greatestFiniteMagnitude),
options: NSStringDrawingOptions.usesLineFragmentOrigin,
attributes: [NSFontAttributeName: self],
context: nil).size
}
}
那就叫它
let width = 290.0
let textSize = UIFont.systemFont(ofSize: 15).sizeOfString("text", constrainedToWidth: width)
删除约束 lblReviewHeight
,然后尝试使用 numberOfLines
控制您的文本布局,如果您想要显示所有描述,请设置 numberOfLines = 0
,否则设置 numberOfLines
你想要的行。