Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

T3TextScale.swift

110 lines3.5 KB

1import SwiftUI
2import UIKit
3​
4enum T3TextSizing {
5 private static let categories: [UIContentSizeCategory] = [
6 .extraSmall,
7 .small,
8 .medium,
9 .large,
10 .extraLarge,
11 .extraExtraLarge,
12 .extraExtraExtraLarge,
13 .accessibilityMedium,
14 .accessibilityLarge,
15 .accessibilityExtraLarge,
16 .accessibilityExtraExtraLarge,
17 .accessibilityExtraExtraExtraLarge,
18 ]
19​
20 static func contentSizeCategory(
21 system: UIContentSizeCategory,
22 steps: Int
23 ) -> UIContentSizeCategory {
24 guard steps != 0, let index = categories.firstIndex(of: system) else { return system }
25 return categories[min(categories.count - 1, max(0, index + steps))]
26 }
28​
29extension DynamicTypeSize {
30 func t3Shifted(by steps: Int) -> DynamicTypeSize {
31 guard steps != 0 else { return self }
32 let sizes = DynamicTypeSize.allCases
33 guard let index = sizes.firstIndex(of: self) else { return self }
34 return sizes[min(sizes.count - 1, max(0, index + steps))]
35 }
37​
38private struct T3CodeSizeStepsKey: EnvironmentKey {
39 static let defaultValue = 0
41​
42extension EnvironmentValues {
43 var t3CodeSizeSteps: Int {
44 get { self[T3CodeSizeStepsKey.self] }
45 set { self[T3CodeSizeStepsKey.self] = newValue }
46 }
48​
49extension View {
50 func t3AppTextSize(steps: Int) -> some View {
51 modifier(T3AppTextSize(steps: steps))
52 }
53​
54 func t3CodeSizing(steps: Int) -> some View {
55 environment(\.t3CodeSizeSteps, steps)
56 }
57​
58 func t3CodeTextSize(_ isEnabled: Bool = true) -> some View {
59 modifier(T3CodeTextSize(isEnabled: isEnabled))
60 }
62​
63/// Uses the window trait so the preference reaches sheets and UIKit-hosted cells.
64/// Removing the override at zero preserves the reader's system Dynamic Type setting.
65private struct T3AppTextSize: ViewModifier {
66 let steps: Int
67 @State private var systemCategory = UIApplication.shared.preferredContentSizeCategory
68​
69 func body(content: Content) -> some View {
70 content
71 .onAppear { apply() }
72 .onChange(of: steps) { _, _ in apply() }
73 .task {
74 for await change in NotificationCenter.default.notifications(
75 named: UIContentSizeCategory.didChangeNotification
76 ) {
77 systemCategory = change.userInfo?[
78 UIContentSizeCategory.newValueUserInfoKey
79 ] as? UIContentSizeCategory
80 ?? UIApplication.shared.preferredContentSizeCategory
81 apply()
82 }
83 }
84 }
85​
86 @MainActor
87 private func apply() {
88 let category = T3TextSizing.contentSizeCategory(system: systemCategory, steps: steps)
89 let windows = UIApplication.shared.connectedScenes
90 .compactMap { $0 as? UIWindowScene }
91 .flatMap(\.windows)
92 for window in windows {
93 if steps == 0 {
94 window.traitOverrides.remove(UITraitPreferredContentSizeCategory.self)
95 } else {
96 window.traitOverrides.preferredContentSizeCategory = category
97 }
98 }
99 }
101​
102private struct T3CodeTextSize: ViewModifier {
103 @SwiftUI.Environment(\.dynamicTypeSize) private var dynamicTypeSize
104 @SwiftUI.Environment(\.t3CodeSizeSteps) private var codeSteps
105 let isEnabled: Bool
106​
107 func body(content: Content) -> some View {
108 content.dynamicTypeSize(dynamicTypeSize.t3Shifted(by: isEnabled ? codeSteps : 0))
109 }