|
1 | 1 | import UIKit
|
2 | 2 |
|
3 |
| -// MARK: - UIKit.UIFont: TextStyle |
4 |
| -public extension TextStyle { |
5 |
| - var uiFont: UIFont { |
6 |
| - switch self { |
7 |
| - case .heading1: |
8 |
| - return UIFont.DS.heading1 |
9 |
| - |
10 |
| - case .heading2: |
11 |
| - return UIFont.DS.heading2 |
12 |
| - |
13 |
| - case .heading3: |
14 |
| - return UIFont.DS.heading3 |
15 |
| - |
16 |
| - case .heading4: |
17 |
| - return UIFont.DS.heading4 |
18 |
| - |
19 |
| - case .bodySmall(let weight): |
20 |
| - switch weight { |
21 |
| - case .regular: |
22 |
| - return UIFont.DS.Body.small |
23 |
| - case .emphasized: |
24 |
| - return UIFont.DS.Body.Emphasized.small |
25 |
| - } |
26 |
| - |
27 |
| - case .bodyMedium(let weight): |
28 |
| - switch weight { |
29 |
| - case .regular: |
30 |
| - return UIFont.DS.Body.medium |
31 |
| - case .emphasized: |
32 |
| - return UIFont.DS.Body.Emphasized.medium |
33 |
| - } |
34 |
| - |
35 |
| - case .bodyLarge(let weight): |
36 |
| - switch weight { |
37 |
| - case .regular: |
38 |
| - return UIFont.DS.Body.large |
39 |
| - case .emphasized: |
40 |
| - return UIFont.DS.Body.Emphasized.large |
41 |
| - } |
42 |
| - |
43 |
| - case .footnote: |
44 |
| - return UIFont.DS.footnote |
45 |
| - |
46 |
| - case .caption: |
47 |
| - return UIFont.DS.caption |
48 |
| - } |
49 |
| - } |
50 |
| -} |
51 |
| - |
52 |
| -// MARK: - SwiftUI.Text |
53 |
| -public extension UILabel { |
54 |
| - func setStyle(_ style: TextStyle) { |
55 |
| - self.font = style.uiFont |
| 3 | +extension UILabel { |
| 4 | + func style(_ style: TextStyle) -> Self { |
| 5 | + self.font = UIFont.DS.font(style) |
56 | 6 | if style.case == .uppercase {
|
57 | 7 | self.text = self.text?.uppercased()
|
58 | 8 | }
|
59 |
| - } |
60 |
| -} |
61 |
| - |
62 |
| -// MARK: - UIKit.UIFont |
63 |
| -fileprivate extension UIFont { |
64 |
| - enum DS { |
65 |
| - static let heading1 = DynamicFontHelper.fontForTextStyle(.largeTitle, fontWeight: .bold) |
66 |
| - static let heading2 = DynamicFontHelper.fontForTextStyle(.title1, fontWeight: .bold) |
67 |
| - static let heading3 = DynamicFontHelper.fontForTextStyle(.title2, fontWeight: .bold) |
68 |
| - static let heading4 = DynamicFontHelper.fontForTextStyle(.title3, fontWeight: .semibold) |
69 |
| - |
70 |
| - enum Body { |
71 |
| - static let small = DynamicFontHelper.fontForTextStyle(.subheadline, fontWeight: .regular) |
72 |
| - static let medium = DynamicFontHelper.fontForTextStyle(.callout, fontWeight: .regular) |
73 |
| - static let large = DynamicFontHelper.fontForTextStyle(.body, fontWeight: .regular) |
74 |
| - |
75 |
| - enum Emphasized { |
76 |
| - static let small = DynamicFontHelper.fontForTextStyle(.subheadline, fontWeight: .semibold) |
77 |
| - static let medium = DynamicFontHelper.fontForTextStyle(.callout, fontWeight: .semibold) |
78 |
| - static let large = DynamicFontHelper.fontForTextStyle(.body, fontWeight: .semibold) |
79 |
| - } |
80 |
| - } |
81 |
| - |
82 |
| - static let footnote = DynamicFontHelper.fontForTextStyle(.footnote, fontWeight: .regular) |
83 |
| - static let caption = DynamicFontHelper.fontForTextStyle(.caption1, fontWeight: .regular) |
84 |
| - } |
85 |
| -} |
86 |
| - |
87 |
| -private enum DynamicFontHelper { |
88 |
| - static func fontForTextStyle(_ style: UIFont.TextStyle, fontWeight weight: UIFont.Weight) -> UIFont { |
89 |
| - /// WORKAROUND: Some font weights scale up well initially but they don't scale up well if dynamic type |
90 |
| - /// is changed in real time. Creating a scaled font offers an alternative solution that works well |
91 |
| - /// even in real time. |
92 |
| - let weightsThatNeedScaledFont: [UIFont.Weight] = [.black, .bold, .heavy, .semibold] |
93 |
| - |
94 |
| - guard !weightsThatNeedScaledFont.contains(weight) else { |
95 |
| - return scaledFont(for: style, weight: weight) |
96 |
| - } |
97 |
| - |
98 |
| - var fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style) |
99 |
| - |
100 |
| - let traits = [UIFontDescriptor.TraitKey.weight: weight] |
101 |
| - fontDescriptor = fontDescriptor.addingAttributes([.traits: traits]) |
102 |
| - |
103 |
| - return UIFont(descriptor: fontDescriptor, size: CGFloat(0.0)) |
104 |
| - } |
105 |
| - |
106 |
| - static func scaledFont(for style: UIFont.TextStyle, weight: UIFont.Weight, design: UIFontDescriptor.SystemDesign = .default) -> UIFont { |
107 |
| - let fontDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: style) |
108 |
| - let fontDescriptorWithDesign = fontDescriptor.withDesign(design) ?? fontDescriptor |
109 |
| - let traits = [UIFontDescriptor.TraitKey.weight: weight] |
110 |
| - let finalDescriptor = fontDescriptorWithDesign.addingAttributes([.traits: traits]) |
111 |
| - |
112 |
| - return UIFont(descriptor: finalDescriptor, size: finalDescriptor.pointSize) |
| 9 | + return self |
113 | 10 | }
|
114 | 11 | }
|
0 commit comments