在一次开发过程中,发现带有行间距的富文本计算高度,会有不准确的情况,富文本内容明明很长,但是计算出的高度只有不到20像素,导致整个cell的高度计算异常。
需求上是文字固定宽度,最多显示3行,超过3行尾部打点展示。按照需求设置了尾部打点 paraStyle.lineBreakMode = .byTruncatingTail, 然后计算富文本的大小。
let range = NSRange(location: 0, length: introduce.count)
var att = NSMutableAttributedString(string: introduce)
let paraStyle = NSMutableParagraphStyle()
paraStyle.lineSpacing = 4
att.addAttribute(.font, value: UIFont.systemFont(ofSize: 14), range: range)
// 计算内容高度
let screenWidth = UIScreen.main.bounds.size.width
let maxSzie = CGSizeMake(screenWidth-40, CGFLOAT_MAX)
// 换行模式byClipping
paraStyle.lineBreakMode = .byTruncatingTail
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byTruncatingTail计算size ", size)
这时候算出来的size高度只有17像素,真实显示到屏幕上Label的高度是58像素。导致整体cell的高度计算错误。
后来发现先将换行模式改为paraStyle.lineBreakMode = .byWordWrapping 在计算高度是正确的。于是把所有的换行模式都逐个测试,看看到底是什么情况。
func getTextAttributed(_ introduce: String) -> NSAttributedString {
let range = NSRange(location: 0, length: introduce.count)
var att = NSMutableAttributedString(string: introduce)
let paraStyle = NSMutableParagraphStyle()
paraStyle.lineSpacing = 4
att.addAttribute(.font, value: UIFont.systemFont(ofSize: 14), range: range)
// 计算内容高度
let screenWidth = UIScreen.main.bounds.size.width
let maxSzie = CGSizeMake(screenWidth-40, CGFLOAT_MAX)
// 换行模式byWordWrapping
paraStyle.lineBreakMode = .byWordWrapping
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
var size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byWordWrapping计算size ", size)
// 换行模式byCharWrapping
paraStyle.lineBreakMode = .byCharWrapping
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byCharWrapping计算size ", size)
// 换行模式byClipping
paraStyle.lineBreakMode = .byClipping
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byClipping计算size ", size)
// 换行模式byTruncatingHead
paraStyle.lineBreakMode = .byTruncatingHead
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byTruncatingHead计算size ", size)
// 换行模式byClipping
paraStyle.lineBreakMode = .byTruncatingTail
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byTruncatingTail计算size ", size)
// 换行模式byClipping
paraStyle.lineBreakMode = .byTruncatingMiddle
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byTruncatingMiddle计算size ", size)
return att
}
测试后发现,系统的5个枚举中,只有 byWordWrapping和byCharWrapping 计算准确,带有截断方式的case,计算都是错误的。
- case byWordWrapping = 0 // Wrap at word boundaries, default
- case byCharWrapping = 1 // Wrap at character boundaries
- case byClipping = 2 // Simply clip
- case byTruncatingHead = 3 // Truncate at head of line: "...wxyz"
- case byTruncatingTail = 4 // Truncate at tail of line: "abcd..."
- case byTruncatingMiddle = 5 // Truncate middle of line: "ab...yz"
好吧,系统这个样子,我们也没有办法,最后采用的方式
- 先用 byWordWrapping 计算高度
- 高度计算完成,在修改换行模式,paraStyle.lineBreakMode = .byTruncatingTail
- 返回富文本
func getTextAttributed(_ introduce: String) -> NSAttributedString {
let range = NSRange(location: 0, length: introduce.count)
let att = NSMutableAttributedString(string: introduce)
let paraStyle = NSMutableParagraphStyle()
paraStyle.lineSpacing = 4
att.addAttribute(.font, value: UIFont.systemFont(ofSize: 14), range: range)
// 计算内容高度
let screenWidth = UIScreen.main.bounds.size.width
let maxSzie = CGSizeMake(screenWidth-40, CGFLOAT_MAX)
// 换行模式byWordWrapping
paraStyle.lineBreakMode = .byWordWrapping
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
var size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
print("使用byWordWrapping计算size ", size)
// 换行模式byTruncatingTail
paraStyle.lineBreakMode = .byTruncatingTail
att.addAttribute(.paragraphStyle, value: paraStyle, range: range)
// size = att.boundingRect(with: maxSzie, options: [.usesLineFragmentOrigin,.usesFontLeading], context: nil).size
// print("使用byTruncatingTail计算size ", size)
return att
}