Skip to content

Commit 4798a51

Browse files
committed
完善Debug段落样式设置
1 parent e100025 commit 4798a51

File tree

5 files changed

+150
-24
lines changed

5 files changed

+150
-24
lines changed

Demo/Demo/Debug/Debug.swift

+35
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ extension Debug {
3232
// paragraphs
3333
var lineSpacing: CGFloat?
3434
var lineHeightMultiple: CGFloat?
35+
var minimumLineHeight: CGFloat?
36+
var maximumLineHeight: CGFloat?
37+
var paragraphSpacing: CGFloat?
38+
var paragraphSpacingBefore: CGFloat?
39+
var firstLineHeadIndent: CGFloat?
40+
var headIndent: CGFloat?
41+
var tailIndent: CGFloat?
3542

3643
init() {
3744
font = .systemFont(ofSize: 17.0)
@@ -50,6 +57,13 @@ extension Debug {
5057
case allowsDefaultTighteningForTruncation
5158
case lineSpacing
5259
case lineHeightMultiple
60+
case minimumLineHeight
61+
case maximumLineHeight
62+
case paragraphSpacing
63+
case paragraphSpacingBefore
64+
case firstLineHeadIndent
65+
case headIndent
66+
case tailIndent
5367
}
5468

5569
init(from decoder: Decoder) throws {
@@ -86,6 +100,20 @@ extension Debug {
86100
self.lineSpacing = try container.decodeIfPresent(CGFloat.self, forKey: .lineSpacing)
87101

88102
self.lineHeightMultiple = try container.decodeIfPresent(CGFloat.self, forKey: .lineHeightMultiple)
103+
104+
self.minimumLineHeight = try container.decodeIfPresent(CGFloat.self, forKey: .minimumLineHeight)
105+
106+
self.maximumLineHeight = try container.decodeIfPresent(CGFloat.self, forKey: .maximumLineHeight)
107+
108+
self.paragraphSpacing = try container.decodeIfPresent(CGFloat.self, forKey: .paragraphSpacing)
109+
110+
self.paragraphSpacingBefore = try container.decodeIfPresent(CGFloat.self, forKey: .paragraphSpacingBefore)
111+
112+
self.firstLineHeadIndent = try container.decodeIfPresent(CGFloat.self, forKey: .firstLineHeadIndent)
113+
114+
self.headIndent = try container.decodeIfPresent(CGFloat.self, forKey: .headIndent)
115+
116+
self.tailIndent = try container.decodeIfPresent(CGFloat.self, forKey: .tailIndent)
89117
}
90118

91119
func encode(to encoder: Encoder) throws {
@@ -102,6 +130,13 @@ extension Debug {
102130
try container.encodeIfPresent(allowsDefaultTighteningForTruncation, forKey: .allowsDefaultTighteningForTruncation)
103131
try container.encodeIfPresent(lineSpacing, forKey: .lineSpacing)
104132
try container.encodeIfPresent(lineHeightMultiple, forKey: .lineHeightMultiple)
133+
try container.encodeIfPresent(minimumLineHeight, forKey: .minimumLineHeight)
134+
try container.encodeIfPresent(maximumLineHeight, forKey: .maximumLineHeight)
135+
try container.encodeIfPresent(paragraphSpacing, forKey: .paragraphSpacing)
136+
try container.encodeIfPresent(paragraphSpacingBefore, forKey: .paragraphSpacingBefore)
137+
try container.encodeIfPresent(firstLineHeadIndent, forKey: .firstLineHeadIndent)
138+
try container.encodeIfPresent(headIndent, forKey: .headIndent)
139+
try container.encodeIfPresent(tailIndent, forKey: .tailIndent)
105140
}
106141
}
107142
}

Demo/Demo/Debug/DebugLabelView.swift

+46
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,45 @@ class DebugLabelView: UIView {
126126
}
127127
}
128128

129+
var paragraphSpacing: CGFloat = 0 {
130+
didSet {
131+
let value = paragraphSpacing
132+
paragraphSpacingLabel.text = String(format: "%.2f", value)
133+
paragraphSpacingSlider.value = .init(value)
134+
}
135+
}
136+
137+
var paragraphSpacingBefore: CGFloat = 0 {
138+
didSet {
139+
let value = paragraphSpacingBefore
140+
paragraphSpacingBeforeLabel.text = String(format: "%.2f", value)
141+
paragraphSpacingBeforeSlider.value = .init(value)
142+
}
143+
}
144+
145+
var firstLineHeadIndent: CGFloat = 0 {
146+
didSet {
147+
let value = firstLineHeadIndent
148+
firstLineHeadIndentLabel.text = String(format: "%.2f", value)
149+
firstLineHeadIndentSlider.value = .init(value)
150+
}
151+
}
152+
var headIndent: CGFloat = 0 {
153+
didSet {
154+
let value = headIndent
155+
headIndentLabel.text = String(format: "%.2f", value)
156+
headIndentSlider.value = .init(value)
157+
}
158+
}
159+
160+
var tailIndent: CGFloat = 0 {
161+
didSet {
162+
let value = tailIndent
163+
tailIndentLabel.text = String(format: "%.2f", value)
164+
tailIndentSlider.value = .init(value)
165+
}
166+
}
167+
129168
@IBOutlet private weak var label: UILabel!
130169

131170
@IBOutlet private weak var widthLayoutConstraint: NSLayoutConstraint!
@@ -252,6 +291,13 @@ extension DebugLabelView {
252291
// paragraphs
253292
lineSpacing = info.lineSpacing ?? 0
254293
lineHeightMultiple = info.lineHeightMultiple ?? 0
294+
minimumLineHeight = info.minimumLineHeight ?? 0
295+
maximumLineHeight = info.maximumLineHeight ?? 0
296+
paragraphSpacing = info.paragraphSpacing ?? 0
297+
paragraphSpacingBefore = info.paragraphSpacingBefore ?? 0
298+
firstLineHeadIndent = info.firstLineHeadIndent ?? 0
299+
headIndent = info.headIndent ?? 0
300+
tailIndent = info.tailIndent ?? 0
255301

256302
// 刷新布局
257303
layoutIfNeeded()

Demo/Demo/Debug/DebugLabelViewController.swift

+66-20
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
2828
super.viewDidLoad()
2929

3030
setup()
31-
update()
31+
updateText()
3232
}
3333

3434
private func setup() {
@@ -41,24 +41,75 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
4141
}
4242

4343
private func set(info: Debug.Label) {
44+
45+
func update(_ style: AttributedString.Attribute.ParagraphStyle) {
46+
paragraphs.removeAll(where: { $0 ~= style })
47+
paragraphs.append(style)
48+
}
49+
50+
func remove(_ style: AttributedString.Attribute.ParagraphStyle) {
51+
paragraphs.removeAll(where: { $0 ~= style })
52+
}
53+
4454
if let value = info.lineSpacing {
45-
paragraphs.removeAll(where: { $0 == .lineSpacing(0) })
46-
paragraphs.append(.lineSpacing(value))
55+
update(.lineSpacing(value))
4756

4857
} else {
49-
paragraphs.removeAll(where: { $0 == .lineSpacing(0) })
58+
remove(.lineSpacing(0))
5059
}
5160
if let value = info.lineHeightMultiple {
52-
paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) })
53-
paragraphs.append(.lineHeightMultiple(value))
61+
update(.lineHeightMultiple(value))
62+
63+
} else {
64+
remove(.lineHeightMultiple(0))
65+
}
66+
if let value = info.minimumLineHeight {
67+
update(.minimumLineHeight(value))
68+
69+
} else {
70+
remove(.minimumLineHeight(0))
71+
}
72+
if let value = info.maximumLineHeight {
73+
update(.maximumLineHeight(value))
74+
75+
} else {
76+
remove(.maximumLineHeight(0))
77+
}
78+
if let value = info.paragraphSpacing {
79+
update(.paragraphSpacing(value))
80+
81+
} else {
82+
remove(.paragraphSpacing(0))
83+
}
84+
if let value = info.paragraphSpacingBefore {
85+
update(.paragraphSpacingBefore(value))
86+
87+
} else {
88+
remove(.paragraphSpacingBefore(0))
89+
}
90+
if let value = info.firstLineHeadIndent {
91+
update(.firstLineHeadIndent(value))
92+
93+
} else {
94+
remove(.firstLineHeadIndent(0))
95+
}
96+
if let value = info.headIndent {
97+
update(.headIndent(value))
98+
99+
} else {
100+
remove(.headIndent(0))
101+
}
102+
if let value = info.tailIndent {
103+
update(.tailIndent(value))
54104

55105
} else {
56-
paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) })
106+
remove(.tailIndent(0))
57107
}
58108
container.set(info: info)
109+
updateText()
59110
}
60111

61-
private func update() {
112+
private func updateText() {
62113
container.set(text: .init(
63114
attributedString,
64115
with: attributes + [.paragraph(paragraphs)]
@@ -121,37 +172,32 @@ class DebugLabelViewController: ViewController<DebugLabelView> {
121172
}
122173

123174
@IBAction func lineSpacingSliderAction(_ sender: UISlider) {
124-
paragraphs.removeAll(where: { $0 == .lineSpacing(0) })
125-
paragraphs.append(.lineSpacing(.init(sender.value)))
126175
info.lineSpacing = .init(sender.value)
127-
update()
128176
}
129177
@IBAction func lineHeightMultipleSliderAction(_ sender: UISlider) {
130-
paragraphs.removeAll(where: { $0 == .lineHeightMultiple(0) })
131-
paragraphs.append(.lineHeightMultiple(.init(sender.value)))
132178
info.lineHeightMultiple = .init(sender.value)
133-
update()
134179
}
135-
136180
@IBAction func minimumLineHeightSliderAction(_ sender: UISlider) {
181+
info.minimumLineHeight = .init(sender.value)
137182
}
138183
@IBAction func maximumLineHeightSliderAction(_ sender: UISlider) {
184+
info.maximumLineHeight = .init(sender.value)
139185
}
140-
141186
@IBAction func paragraphSpacingSliderAction(_ sender: UISlider) {
187+
info.paragraphSpacing = .init(sender.value)
142188
}
143189
@IBAction func paragraphSpacingBeforeSliderAction(_ sender: UISlider) {
190+
info.paragraphSpacingBefore = .init(sender.value)
144191
}
145-
146192
@IBAction func firstLineHeadIndentSliderAction(_ sender: UISlider) {
193+
info.firstLineHeadIndent = .init(sender.value)
147194
}
148-
149195
@IBAction func headIndentSliderAction(_ sender: UISlider) {
196+
info.headIndent = .init(sender.value)
150197
}
151-
152198
@IBAction func tailIndentSliderAction(_ sender: UISlider) {
199+
info.tailIndent = .init(sender.value)
153200
}
154-
155201
}
156202

157203
extension DebugLabelViewController: UIScrollViewDelegate {

Sources/Extension/UIKit/UILabel/UILabelExtension.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ fileprivate extension UILabel {
270270
guard let attributedString = AttributedString(text) else { return nil }
271271

272272
// 构建同步Label的TextKit
273-
// 注: 目前还剩一个截断处理没解决 比如 "a\n\n\nb" numberOfLines=2
274273
let delegate = UILabelLayoutManagerDelegate(scaledMetrics, with: baselineAdjustment)
275274
let textStorage = NSTextStorage()
276275
let textContainer = NSTextContainer(size: bounds.size)
@@ -450,7 +449,7 @@ extension UILabel {
450449
) { (value, range, stop) in
451450
guard let old = value as? NSParagraphStyle else { return }
452451
guard let new = old.mutableCopy() as? NSMutableParagraphStyle else { return }
453-
new.lineBreakMode = .byWordWrapping
452+
new.lineBreakMode = numberOfLines == 1 ? .byCharWrapping : .byWordWrapping
454453
if #available(iOS 11.0, *) {
455454
new.setValue(1, forKey: "lineBreakStrategy")
456455
}

Sources/ParagraphStyle.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ extension AttributedString.Attribute.ParagraphStyle {
157157
}
158158
}
159159

160-
extension AttributedString.Attribute.ParagraphStyle: Equatable {
160+
extension AttributedString.Attribute.ParagraphStyle {
161161

162-
public static func == (lhs: AttributedString.Attribute.ParagraphStyle, rhs: AttributedString.Attribute.ParagraphStyle) -> Bool {
162+
public static func ~= (lhs: AttributedString.Attribute.ParagraphStyle, rhs: AttributedString.Attribute.ParagraphStyle) -> Bool {
163163
return lhs.style.keys == rhs.style.keys
164164
}
165165
}

0 commit comments

Comments
 (0)