Skip to content

Commit 23608f7

Browse files
committed
优化代码
1 parent af5b636 commit 23608f7

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

Demo/Demo/Debug/DebugLabelView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,15 @@ class DebugLabelView: UIView {
9696
didSet {
9797
let value = lineSpacing
9898
lineSpacingLabel.text = String(format: "%.2f", value)
99+
lineSpacingSlider.value = .init(value)
99100
}
100101
}
101102

102103
var lineHeightMultiple: CGFloat = 0 {
103104
didSet {
104105
let value = lineHeightMultiple
105106
lineHeightMultipleLabel.text = String(format: "%.2f", value)
107+
lineHeightMultipleSlider.value = .init(value)
106108
}
107109
}
108110

Demo/Demo/Debug/DebugLabelViewController.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
1515

1616
private var info: Debug.Label = .init() {
1717
didSet {
18+
if let value = info.lineSpacing {
19+
paragraphs.removeAll(where: { $0 == .lineSpacing(0) })
20+
paragraphs.append(.lineSpacing(value))
21+
22+
} else {
23+
paragraphs.removeAll(where: { $0 == .lineSpacing(0) })
24+
}
25+
if let value = info.lineHeightMultiple {
26+
paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) })
27+
paragraphs.append(.lineHeightMultiple(value))
28+
29+
} else {
30+
paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) })
31+
}
1832
container.set(info: info)
1933
}
2034
}
@@ -108,11 +122,13 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
108122
}
109123

110124
@IBAction func lineSpacingSliderAction(_ sender: UISlider) {
125+
paragraphs.removeAll(where: { $0 == .lineSpacing(0) })
111126
paragraphs.append(.lineSpacing(.init(sender.value)))
112127
info.lineSpacing = .init(sender.value)
113128
update()
114129
}
115130
@IBAction func lineHeightMultipleSliderAction(_ sender: UISlider) {
131+
paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) })
116132
paragraphs.append(.lineHeightMultiple(.init(sender.value)))
117133
info.lineHeightMultiple = .init(sender.value)
118134
update()

Sources/Extension/UIKit/UILabel/UILabelLayoutManagerDelegate.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,8 @@ class UILabelLayoutManagerDelegate: NSObject, NSLayoutManagerDelegate {
6565

6666
var rect = lineFragmentRect.pointee
6767
var used = lineFragmentUsedRect.pointee
68-
// 以最大的高度为准 (可解决附件问题), 同时根据最大行数是否为1来判断used是否需要增加行间距, 以解决1行时应该无行间距的问题.
69-
let temp = max(maximum.lineHeight, used.height)
70-
rect.size.height = temp + maximum.lineSpacing + paragraphSpacing + paragraphSpacingBefore
71-
used.size.height = textContainer.maximumNumberOfLines == 1 ? temp : temp + maximum.lineSpacing
7268

73-
// 当Label发生Scaled时 最大行数为1时 基线偏移不会按比例计算
69+
// 当Label发生Scaled时 最大行数为1时
7470
if let scaledMetrics = scaledMetrics, textContainer.maximumNumberOfLines == 1 {
7571
switch baselineAdjustment {
7672
case .alignBaselines:
@@ -103,6 +99,12 @@ class UILabelLayoutManagerDelegate: NSObject, NSLayoutManagerDelegate {
10399
default:
104100
break
105101
}
102+
103+
} else {
104+
// 以最大的高度为准 (可解决附件问题), 同时根据最大行数是否为1来判断used是否需要增加行间距, 以解决1行时应该无行间距的问题.
105+
let temp = max(maximum.lineHeight, used.height)
106+
rect.size.height = temp + maximum.lineSpacing + paragraphSpacing + paragraphSpacingBefore
107+
used.size.height = temp
106108
}
107109

108110
// 重新赋值最终结果

Sources/ParagraphStyle.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,10 @@ extension AttributedString.Attribute.ParagraphStyle {
156156
return .init(style: [.allowsDefaultTighteningForTruncation: value])
157157
}
158158
}
159+
160+
extension AttributedString.Attribute.ParagraphStyle: Equatable {
161+
162+
public static func == (lhs: AttributedString.Attribute.ParagraphStyle, rhs: AttributedString.Attribute.ParagraphStyle) -> Bool {
163+
return lhs.style.keys == rhs.style.keys
164+
}
165+
}

0 commit comments

Comments
 (0)