Skip to content

Commit ed5c861

Browse files
committed
基本完成, 个别极端情况待测试.
1 parent 8707b83 commit ed5c861

3 files changed

Lines changed: 11 additions & 7 deletions

File tree

AttributedString.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Pod::Spec.new do |s|
22

33
s.name = "AttributedString"
4-
s.version = "1.6.2"
4+
s.version = "1.6.3"
55
s.summary = "基于Swift字符串插值快速构建你想要的富文本, 支持点击按住等事件获取, 支持多种类型过滤"
66

77
s.homepage = "https://github.com/lixiang1994/AttributedString"

Sources/Extension/UIKit/UILabel/UILabelExtension.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,15 +333,17 @@ fileprivate extension UILabel {
333333
guard let attributedString = AttributedString(text) else { return nil }
334334

335335
// 构建同步Label设置的TextKit
336-
let textStorage = NSTextStorage(attributedString: attributedString.value)
336+
let textStorage = NSTextStorage()
337337
let textContainer = NSTextContainer(size: .init(bounds.size.width, bounds.size.height))
338338
let layoutManager = NSLayoutManager()
339-
layoutManager.delegate = UILabelLayoutManagerDelegate.shared // 重新计算行高确保TextKit与UILabel显示一致
339+
layoutManager.delegate = UILabelLayoutManagerDelegate.shared // 重新计算行高确保TextKit与UILabel显示同步
340340
textContainer.lineBreakMode = lineBreakMode
341341
textContainer.lineFragmentPadding = 0.0
342342
textContainer.maximumNumberOfLines = numberOfLines
343+
layoutManager.usesFontLeading = false // UILabel没有使用FontLeading排版
343344
layoutManager.addTextContainer(textContainer)
344345
textStorage.addLayoutManager(layoutManager)
346+
textStorage.setAttributedString(attributedString.value) // 放在最后添加富文本 TextKit的坑
345347

346348
// 确保布局
347349
layoutManager.ensureLayout(for: textContainer)

Sources/Extension/UIKit/UILabel/UILabelLayoutManagerDelegate.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

149150
extension NSAttributedString.Key {
150151

152+
/// 参考: https://juejin.im/post/6844903838252531725
151153
static let originalFont: NSAttributedString.Key = .init("NSOriginalFont")
152154
}
153155

0 commit comments

Comments
 (0)