-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathUILabel+Typograpy.swift
255 lines (212 loc) · 6.83 KB
/
UILabel+Typograpy.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// UILabel+Typograpy.swift
// UILabel_Typography_Extensions
//
// Copyright © 2020. Geri Borbás. All rights reserved.
// https://twitter.com/Geri_Borbas
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
import UIKit
import SwiftUI
extension Optional where Wrapped == String {
var count: Int {
switch self {
case .none:
return 0
case .some(let wrapped):
return wrapped.count
}
}
}
extension UILabel: TypographyExtensions {
var paragraphStyle: NSParagraphStyle? {
getAttribute(.paragraphStyle)
}
public var lineHeight: CGFloat? {
get { paragraphStyle?.maximumLineHeight }
set {
let lineHeight = newValue ?? font.lineHeight
let adjustment = lineHeight > font.lineHeight ? 2.0 : 1.0
let baselineOffset = (lineHeight - font.lineHeight) / 2.0 / adjustment
addAttribute(.baselineOffset, value: baselineOffset)
addAttribute(
.paragraphStyle,
value: (paragraphStyle ?? NSParagraphStyle())
.mutable
.withProperty(lineHeight, for: \.minimumLineHeight)
.withProperty(lineHeight, for: \.maximumLineHeight)
)
setupAttributeCacheIfNeeded()
}
}
func setupAttributeCacheIfNeeded() {
onTextChange { [unowned self] oldText, newText in
// Apply cached attributes (if any) in case text have just changed from empty.
if oldText.count == 0,
newText.count > 0,
let newText = newText {
let alignment = textAlignment
self.attributedText = NSAttributedString(string: newText, attributes: cachedAttributes)
self.textAlignment = alignment
}
// Update attributed string layout due to (unknown) UIKit internals.
_ = self.attributedText
}
}
public var letterSpacing: CGFloat? {
get { getAttribute(.kern) }
set {
setAttribute(.kern, value: newValue)
setupAttributeCacheIfNeeded()
}
}
public var underline: NSUnderlineStyle? {
get { getAttribute(.underlineStyle) }
set {
setAttribute(.underlineStyle, value: newValue)
setupAttributeCacheIfNeeded()
}
}
public var strikethrough: NSUnderlineStyle? {
get { getAttribute(.strikethroughStyle) }
set {
setAttribute(.strikethroughStyle, value: newValue)
setupAttributeCacheIfNeeded()
}
}
public var leadingImage: Typography.Image? {
get { nil }
set { }
}
public var trailingImage: Typography.Image? {
get { nil }
set { }
}
}
// MARK: Attributes (and caching)
fileprivate extension NSAttributedString {
var entireRange: NSRange {
NSRange(location: 0, length: self.length)
}
func stringByAddingAttribute(_ key: NSAttributedString.Key, value: Any) -> NSAttributedString {
let changedString = NSMutableAttributedString(attributedString: self)
changedString.addAttribute(key, value: value, range: self.entireRange)
return changedString
}
func stringByRemovingAttribute(_ key: NSAttributedString.Key) -> NSAttributedString {
let changedString = NSMutableAttributedString(attributedString: self)
changedString.removeAttribute(key, range: self.entireRange)
return changedString
}
}
fileprivate extension UILabel {
struct Keys {
static var cache: UInt8 = 0
}
/// An attributed string property to cache typography even when the label text is empty.
var cache: NSAttributedString {
get {
objc_getAssociatedObject(self, &Keys.cache) as? NSAttributedString ?? NSAttributedString(string: "Placeholder")
}
set {
objc_setAssociatedObject(self, &Keys.cache, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
}
}
/// Attributes of `attributedText` (if any).
var attributes: [NSAttributedString.Key: Any]? {
get {
if let attributedText = attributedText,
attributedText.length > 0 {
return attributedText.attributes(at: 0, effectiveRange: nil)
} else {
return nil
}
}
}
/// Attributes of `cache`.
var cachedAttributes: [NSAttributedString.Key: Any] {
cache.attributes(at: 0, effectiveRange: nil)
}
func addAttribute(_ key: NSAttributedString.Key, value: Any) {
attributedText = attributedText?.stringByAddingAttribute(key, value: value)
cache = cache.stringByAddingAttribute(key, value: value)
}
func removeAttribute(_ key: NSAttributedString.Key) {
attributedText = attributedText?.stringByRemovingAttribute(key)
cache = cache.stringByRemovingAttribute(key)
}
}
fileprivate extension UILabel {
/// Get attribute for the given key (if any).
func getAttribute<AttributeType>(
_ key: NSAttributedString.Key
) -> AttributeType? where AttributeType: Any {
return (attributes ?? cachedAttributes)[key] as? AttributeType
}
/// Get `OptionSet` attribute for the given key (if any).
func getAttribute<AttributeType>(
_ key: NSAttributedString.Key
) -> AttributeType? where AttributeType: OptionSet {
if let attribute = (attributes ?? cachedAttributes)[key] as? AttributeType.RawValue {
return .init(rawValue: attribute)
} else {
return nil
}
}
/// Add (or remove) attribute for the given key (if any).
func setAttribute<AttributeType>(
_ key: NSAttributedString.Key,
value: AttributeType?
) where AttributeType: Any {
if let value = value {
addAttribute(key, value: value)
} else {
removeAttribute(key)
}
}
/// Add (or remove) `OptionSet` attribute for the given key (if any).
func setAttribute<AttributeType>(
_ key: NSAttributedString.Key,
value: AttributeType?
) where AttributeType: OptionSet {
if let value = value {
addAttribute(key, value: value.rawValue)
} else {
removeAttribute(key)
}
}
}
// MARK: Paragraph Style
fileprivate extension NSParagraphStyle {
var mutable: NSMutableParagraphStyle {
let mutable = NSMutableParagraphStyle()
mutable.setParagraphStyle(self)
return mutable
}
}
fileprivate extension NSMutableParagraphStyle {
func withProperty<ValueType>(
_ value: ValueType,
for keyPath: ReferenceWritableKeyPath<NSMutableParagraphStyle, ValueType>
) -> NSMutableParagraphStyle {
self[keyPath: keyPath] = value
return self
}
}