forked from RedMadRobot/figma-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Label.swift
116 lines (93 loc) · 2.72 KB
/
Label.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
//
// The code generated using FigmaExport — Command line utility to export
// colors, typography, icons and images from Figma to Xcode project.
//
// https://github.com/RedMadRobot/figma-export
//
// Don’t edit this code manually to avoid runtime crashes
//
import UIKit
public class Label: UILabel {
var style: LabelStyle? { nil }
public override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
super.traitCollectionDidChange(previousTraitCollection)
if previousTraitCollection?.preferredContentSizeCategory != traitCollection.preferredContentSizeCategory {
updateText()
}
}
convenience init(text: String?, textColor: UIColor) {
self.init()
self.text = text
self.textColor = textColor
}
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
updateText()
}
private func commonInit() {
font = style?.font
adjustsFontForContentSizeCategory = true
}
private func updateText() {
text = super.text
}
public override var text: String? {
get {
guard style?.attributes != nil else {
return super.text
}
return attributedText?.string
}
set {
guard let style = style else {
super.text = newValue
return
}
guard let newText = newValue else {
attributedText = nil
super.text = nil
return
}
let attributes = style.attributes(for: textAlignment, lineBreakMode: lineBreakMode)
attributedText = NSAttributedString(string: newText, attributes: attributes)
}
}
}
public final class LargeTitleLabel: Label {
override var style: LabelStyle? {
LabelStyle(
font: UIFont.largeTitle(),
fontMetrics: UIFontMetrics(forTextStyle: .largeTitle)
)
}
}
public final class HeaderLabel: Label {
override var style: LabelStyle? {
LabelStyle(
font: UIFont.header()
)
}
}
public final class BodyLabel: Label {
override var style: LabelStyle? {
LabelStyle(
font: UIFont.body(),
fontMetrics: UIFontMetrics(forTextStyle: .body),
lineHeight: 24.0
)
}
}
public final class CaptionLabel: Label {
override var style: LabelStyle? {
LabelStyle(
font: UIFont.caption(),
fontMetrics: UIFontMetrics(forTextStyle: .footnote),
lineHeight: 20.0
)
}
}