Skip to content

Commit 39660a3

Browse files
committed
0.9.0
1 parent 1850086 commit 39660a3

4 files changed

Lines changed: 31 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
+ (May) lookup baseline reset issue with overflowing text
66

7+
* 0.9.0
8+
9+
+ Cleanup, renamings
10+
711
* Feature/Empty_Label_Support/0.2.0 - 0.2.1
812

913
+ Added `oldText` and `newText` parameters to `onTextChange` observer

UILabel_Typography_Extensions/Typography/UILabel+Observer.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,17 @@ extension UILabel {
4545
}
4646
}
4747

48-
func onTextChange(onChange completion: @escaping TextChangeAction) {
48+
func onTextChange(_ completion: @escaping TextChangeAction) {
4949
guard observer == nil else {
5050
return
5151
}
5252

5353
observer = TextObserver(
5454
for: self,
5555
keyPath: \.text,
56-
onChange: { oldText, newText in completion(oldText ?? nil, newText ?? nil) }
56+
onChange: { oldText, newText in
57+
completion(oldText ?? nil, newText ?? nil)
58+
}
5759
)
5860
}
5961
}

UILabel_Typography_Extensions/Typography/UILabel+Typograpy.swift

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ extension UILabel: TypographyExtensions {
5757
.paragraphStyle,
5858
value: (paragraphStyle ?? NSParagraphStyle())
5959
.mutable
60-
// .withProperty(textAlignment, for: \.alignment)
60+
// .withProperty(textAlignment, for: \.alignment)
6161
.withProperty(lineHeight, for: \.minimumLineHeight)
6262
.withProperty(lineHeight, for: \.maximumLineHeight)
6363
)
64-
setupCache()
64+
setupAttributeCacheIfNeeded()
6565
}
6666
}
6767

68-
func setupCache() {
68+
func setupAttributeCacheIfNeeded() {
6969
onTextChange { [unowned self] oldText, newText in
7070

7171
// Apply cached attributes (if any) in case text have just changed from empty.
@@ -86,23 +86,23 @@ extension UILabel: TypographyExtensions {
8686
get { getAttribute(.kern) }
8787
set {
8888
setAttribute(.kern, value: newValue)
89-
setupCache()
89+
setupAttributeCacheIfNeeded()
9090
}
9191
}
9292

9393
public var underline: NSUnderlineStyle? {
9494
get { getAttribute(.underlineStyle) }
9595
set {
9696
setAttribute(.underlineStyle, value: newValue)
97-
setupCache()
97+
setupAttributeCacheIfNeeded()
9898
}
9999
}
100100

101101
public var strikethrough: NSUnderlineStyle? {
102102
get { getAttribute(.strikethroughStyle) }
103103
set {
104104
setAttribute(.strikethroughStyle, value: newValue)
105-
setupCache()
105+
setupAttributeCacheIfNeeded()
106106
}
107107
}
108108

@@ -118,6 +118,8 @@ extension UILabel: TypographyExtensions {
118118
}
119119

120120

121+
// MARK: Attributes (and caching)
122+
121123
fileprivate extension NSAttributedString {
122124

123125
var entireRange: NSRange {
@@ -138,21 +140,19 @@ fileprivate extension NSAttributedString {
138140
}
139141

140142

141-
// MARK: Attributes
142-
143143
fileprivate extension UILabel {
144144

145145
struct Keys {
146-
static var placeholder: UInt8 = 0
146+
static var cache: UInt8 = 0
147147
}
148148

149149
/// An attributed string property to cache typography even when the label text is empty.
150150
var cache: NSAttributedString {
151151
get {
152-
objc_getAssociatedObject(self, &Keys.placeholder) as? NSAttributedString ?? NSAttributedString(string: "Placeholder")
152+
objc_getAssociatedObject(self, &Keys.cache) as? NSAttributedString ?? NSAttributedString(string: "Placeholder")
153153
}
154154
set {
155-
objc_setAssociatedObject(self, &Keys.placeholder, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
155+
objc_setAssociatedObject(self, &Keys.cache, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
156156
}
157157
}
158158

@@ -185,17 +185,17 @@ fileprivate extension UILabel {
185185
}
186186

187187

188-
extension UILabel {
188+
fileprivate extension UILabel {
189189

190190
/// Get attribute for the given key (if any).
191-
fileprivate func getAttribute<AttributeType>(
191+
func getAttribute<AttributeType>(
192192
_ key: NSAttributedString.Key
193193
) -> AttributeType? where AttributeType: Any {
194194
return (attributes ?? cachedAttributes)[key] as? AttributeType
195195
}
196196

197197
/// Get `OptionSet` attribute for the given key (if any).
198-
fileprivate func getAttribute<AttributeType>(
198+
func getAttribute<AttributeType>(
199199
_ key: NSAttributedString.Key
200200
) -> AttributeType? where AttributeType: OptionSet {
201201
if let attribute = (attributes ?? cachedAttributes)[key] as? AttributeType.RawValue {
@@ -206,7 +206,7 @@ extension UILabel {
206206
}
207207

208208
/// Add (or remove) attribute for the given key (if any).
209-
fileprivate func setAttribute<AttributeType>(
209+
func setAttribute<AttributeType>(
210210
_ key: NSAttributedString.Key,
211211
value: AttributeType?
212212
) where AttributeType: Any {
@@ -218,7 +218,7 @@ extension UILabel {
218218
}
219219

220220
/// Add (or remove) `OptionSet` attribute for the given key (if any).
221-
fileprivate func setAttribute<AttributeType>(
221+
func setAttribute<AttributeType>(
222222
_ key: NSAttributedString.Key,
223223
value: AttributeType?
224224
) where AttributeType: OptionSet {
@@ -231,7 +231,9 @@ extension UILabel {
231231
}
232232

233233

234-
extension NSParagraphStyle {
234+
// MARK: Paragraph Style
235+
236+
fileprivate extension NSParagraphStyle {
235237

236238
var mutable: NSMutableParagraphStyle {
237239
let mutable = NSMutableParagraphStyle()
@@ -241,7 +243,7 @@ extension NSParagraphStyle {
241243
}
242244

243245

244-
extension NSMutableParagraphStyle {
246+
fileprivate extension NSMutableParagraphStyle {
245247

246248
func withProperty<ValueType>(
247249
_ value: ValueType,

UILabel_Typography_Extensions/View/EmptyViewController.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ class EmptyViewController: UIViewController {
2121
$0.layer.compositingFilter = "multiplyBlendMode"
2222
$0.numberOfLines = 0
2323
$0.showGrid = true
24+
25+
// Alignment before typography.
26+
$0.textAlignment = .right
2427
$0.lineHeight = CGFloat(87)
2528
$0.underline = .single
2629

2730
// Set text after.
2831
$0.text = loremIpsum
29-
$0.textAlignment = .right
3032

3133
}.inspect
3234

0 commit comments

Comments
 (0)