@@ -57,8 +57,10 @@ class UILabelLayoutManagerDelegate: NSObject, NSLayoutManagerDelegate {
5757
5858 var rect = lineFragmentRect. pointee
5959 var used = lineFragmentUsedRect. pointee
60- used. size. height = max ( maximum. lineHeight, used. height)
61- rect. size. height = used. height + maximum. lineSpacing + paragraphSpacing + paragraphSpacingBefore
60+ // 以最大的高度为准 (可解决附件问题), 同时根据最大行数是否为1来判断rect和used是否需要一致, 以解决1行数多余的行间距问题.
61+ let temp = max ( maximum. lineHeight, used. height)
62+ rect. size. height = temp + maximum. lineSpacing + paragraphSpacing + paragraphSpacingBefore
63+ used. size. height = textContainer. maximumNumberOfLines == 1 ? temp : rect. height
6264
6365 // 重新赋值最终结果
6466 lineFragmentRect. pointee = rect
@@ -98,8 +100,7 @@ extension UILabelLayoutManagerDelegate {
98100 var paragraph : NSParagraphStyle ?
99101 textStorage. enumerateAttributes ( in: characterRange, options: . longestEffectiveRangeNotRequired) {
100102 ( attributes, range, stop) in
101- // 实际计算使用的是 NSOriginalFont lineHeight.
102- print ( attributes [ . originalFont] )
103+ // 使用 NSOriginalFont 的行高进行计算 https://juejin.im/post/6844903838252531725
103104 guard let font = ( attributes [ . originalFont] ?? attributes [ . font] ) as? UIFont else { return }
104105 paragraph = paragraph ?? attributes [ . paragraphStyle] as? NSParagraphStyle
105106
@@ -148,6 +149,7 @@ extension UILabelLayoutManagerDelegate {
148149
149150extension NSAttributedString . Key {
150151
152+ /// 参考: https://juejin.im/post/6844903838252531725
151153 static let originalFont : NSAttributedString . Key = . init( " NSOriginalFont " )
152154}
153155
0 commit comments