-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathUIFontExtensions.swift
More file actions
196 lines (181 loc) · 9.4 KB
/
UIFontExtensions.swift
File metadata and controls
196 lines (181 loc) · 9.4 KB
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
//
// This file is part of Canvas.
// Copyright (C) 2018-present Instructure, Inc.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
import UIKit
import SwiftUI
public extension UIFont {
enum Name: String, CaseIterable {
case regular10, regular11Monodigit, regular12, regular13, regular14, regular14Italic, regular15, regular16, regular17, regular20, regular22, regular23, regular24, regular20Monodigit, regular30
case medium10, medium12, medium14, medium16, medium20
case semibold10, semibold11, semibold12, semibold13, semibold14, semibold16, semibold17, semibold16Italic, semibold18, semibold20, semibold22, semibold23, semibold28, semibold38
case bold10, bold11, bold12, bold13, bold14, bold15, bold16, bold17, bold20, bold22, bold24, bold34
case heavy24
}
/// Get a named font style, that is dynamically scaled.
///
/// See https://developer.apple.com/design/human-interface-guidelines/ios/visual-design/typography/
static func scaledNamedFont(_ name: Name) -> UIFont {
switch name {
case .regular10:
return scaledFont(.caption2, for: applicationFont(ofSize: 10, weight: .regular))
case .regular11Monodigit:
return scaledFont(.caption1, for: .monospacedApplicationFont(ofSize: 11, weight: .regular))
case .regular12:
return scaledFont(.caption1, for: applicationFont(ofSize: 12, weight: .regular))
case .regular13:
return scaledFont(.caption1, for: applicationFont(ofSize: 13, weight: .regular))
case .regular14:
return scaledFont(.body, for: applicationFont(ofSize: 14, weight: .regular))
case .regular14Italic:
return scaledFont(.body, for: applicationFont(ofSize: 14, weight: .regular, isItalic: true), traits: .traitItalic)
case .regular15:
return scaledFont(.body, for: applicationFont(ofSize: 15, weight: .regular))
case .regular16:
return scaledFont(.body, for: applicationFont(ofSize: 16, weight: .regular))
case .regular17:
return scaledFont(.body, for: applicationFont(ofSize: 17, weight: .regular))
case .regular20:
return scaledFont(.callout, for: applicationFont(ofSize: 20, weight: .regular))
case .regular20Monodigit:
return scaledFont(.title3, for: .monospacedApplicationFont(ofSize: 20, weight: .regular))
case .regular22:
return scaledFont(.body, for: applicationFont(ofSize: 22, weight: .regular))
case .regular23:
return scaledFont(.body, for: applicationFont(ofSize: 23, weight: .regular))
case .regular24:
return scaledFont(.body, for: applicationFont(ofSize: 24, weight: .regular))
case .regular30:
return scaledFont(.title1, for: applicationFont(ofSize: 30, weight: .regular))
case .medium10:
return scaledFont(.title3, for: applicationFont(ofSize: 10, weight: .medium))
case .medium12:
return scaledFont(.caption1, for: applicationFont(ofSize: 12, weight: .medium))
case .medium14:
return scaledFont(.body, for: applicationFont(ofSize: 14, weight: .medium))
case .medium16:
return scaledFont(.title2, for: applicationFont(ofSize: 16, weight: .medium))
case .medium20:
return scaledFont(.title1, for: applicationFont(ofSize: 20, weight: .medium))
case .semibold10:
return scaledFont(.body, for: applicationFont(ofSize: 10, weight: .semibold))
case .semibold11:
return scaledFont(.body, for: applicationFont(ofSize: 11, weight: .semibold))
case .semibold12:
return scaledFont(.body, for: applicationFont(ofSize: 12, weight: .semibold))
case .semibold13:
return scaledFont(.body, for: applicationFont(ofSize: 13, weight: .semibold))
case .semibold14:
return scaledFont(.callout, for: applicationFont(ofSize: 14, weight: .semibold))
case .semibold16:
return scaledFont(.callout, for: applicationFont(ofSize: 16, weight: .semibold))
case .semibold16Italic:
return scaledFont(.callout, for: applicationFont(ofSize: 16, weight: .semibold, isItalic: true), traits: .traitItalic)
case .semibold17:
return scaledFont(.callout, for: applicationFont(ofSize: 17, weight: .semibold))
case .semibold18:
return scaledFont(.title2, for: applicationFont(ofSize: 18, weight: .semibold))
case .semibold20:
return scaledFont(.title3, for: applicationFont(ofSize: 20, weight: .semibold))
case .semibold22:
return scaledFont(.title3, for: applicationFont(ofSize: 22, weight: .semibold))
case .semibold23:
return scaledFont(.title3, for: applicationFont(ofSize: 23, weight: .semibold))
case .semibold28:
return scaledFont(.title3, for: applicationFont(ofSize: 28, weight: .semibold))
case .semibold38:
return scaledFont(.title3, for: applicationFont(ofSize: 38, weight: .semibold))
case .bold10:
return scaledFont(.body, for: applicationFont(ofSize: 10, weight: .bold))
case .bold11:
return scaledFont(.body, for: applicationFont(ofSize: 11, weight: .bold))
case .bold12:
return scaledFont(.body, for: applicationFont(ofSize: 12, weight: .bold))
case .bold13:
return scaledFont(.body, for: applicationFont(ofSize: 13, weight: .bold))
case .bold14:
return scaledFont(.title2, for: applicationFont(ofSize: 14, weight: .bold))
case .bold15:
return scaledFont(.title2, for: applicationFont(ofSize: 15, weight: .bold))
case .bold16:
return scaledFont(.title2, for: applicationFont(ofSize: 16, weight: .bold))
case .bold17:
return scaledFont(.title2, for: applicationFont(ofSize: 17, weight: .bold))
case .bold20:
return UIFontMetrics(forTextStyle: .title3).scaledFont(for: applicationFont(ofSize: 20, weight: .bold))
case .bold22:
return UIFontMetrics(forTextStyle: .title3).scaledFont(for: applicationFont(ofSize: 22, weight: .bold))
case .bold24:
return scaledFont(.largeTitle, for: applicationFont(ofSize: 24, weight: .bold))
case .bold34:
return scaledFont(.largeTitle, for: applicationFont(ofSize: 34, weight: .bold))
case .heavy24:
return scaledFont(.title1, for: applicationFont(ofSize: 24, weight: .heavy))
}
}
static func monospacedApplicationFont(ofSize fontSize: CGFloat, weight: UIFont.Weight) -> UIFont {
if AppEnvironment.shared.k5.isK5Enabled {
return applicationFont(ofSize: fontSize, weight: weight)
} else {
return .monospacedDigitSystemFont(ofSize: fontSize, weight: weight)
}
}
static func applicationFontName(weight: UIFont.Weight, isItalic: Bool = false) -> String {
let isK5Font = AppEnvironment.shared.k5.isK5Enabled
let font = isK5Font ? "BalsamiqSans" : "Lato"
var suffix = ""
if isK5Font {
switch weight {
case .black, .heavy:
suffix = "Bold"
default:
suffix = "Regular"
}
} else {
switch weight {
case .black, .heavy:
suffix = "Black"
case .bold, .medium:
suffix = isItalic ? "BoldItalic" : "Bold"
case .semibold:
suffix = isItalic ? "SemiBoldItalic" : "SemiBold"
case .regular, .light, .thin, .ultraLight:
suffix = isItalic ? "Italic" : "Regular"
default:
suffix = "Regular"
}
}
return "\(font)-\(suffix)"
}
static func applicationFont(ofSize fontSize: CGFloat, weight: UIFont.Weight, isItalic: Bool = false) -> UIFont {
return UIFont(
name: applicationFontName(weight: weight, isItalic: isItalic),
size: fontSize
)!
}
private static func scaledFont(_ style: TextStyle, for font: UIFont, traits: UIFontDescriptor.SymbolicTraits? = nil) -> UIFont {
guard let traits = traits else {
return UIFontMetrics(forTextStyle: style).scaledFont(for: font)
}
let descriptor = font.fontDescriptor
// If `font` doesn't support the requested traits the `withSymbolicTraits` method returns nil.
guard let descriptorWithSymbolicTraits = descriptor.withSymbolicTraits(descriptor.symbolicTraits.union(traits)) else {
return UIFontMetrics(forTextStyle: style).scaledFont(for: font)
}
let font = UIFont(descriptor: descriptorWithSymbolicTraits, size: descriptor.pointSize)
return UIFontMetrics(forTextStyle: style).scaledFont(for: font)
}
}