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
import CoreText
import Foundation
import SwiftUI
import UIKit
struct DisplayPreferences {
var hideAvatars = false
var handlesOnly = false
func name(_ author: Post.Author) -> String {
name(displayName: author.name, handle: author.handle)
}
func name(displayName: String, handle: String) -> String {
handlesOnly ? "@\(handle)" : displayName
}
}
enum AppFont: String, CaseIterable, Identifiable {
case system
case atkinson
case iosevka
case paperMono
var id: String { rawValue }
var title: String {
switch self {
case .system: "System"
case .atkinson: "Atkinson Hyperlegible Next"
case .iosevka: "Iosevka"
case .paperMono: "Paper Mono"
}
}
var fontName: String? {
switch self {
case .system: nil
case .atkinson: "AtkinsonHyperlegibleNext-Regular"
case .iosevka: "Iosevka"
case .paperMono: "PaperMono-Regular"
}
}
static func registerBundledFonts() {
guard let urls = Bundle.main.urls(forResourcesWithExtension: "ttf", subdirectory: "Fonts") else { return }
for url in urls {
CTFontManagerRegisterFontsForURL(url as CFURL, .process, nil)
}
}
func font(_ style: Font.TextStyle, weight: Font.Weight? = nil) -> Font {
guard let fontName else {
let font = Font.system(style)
return weight.map { font.weight($0) } ?? font
}
let size = UIFont.preferredFont(
forTextStyle: style.uiStyle,
compatibleWith: UITraitCollection(preferredContentSizeCategory: .large)
).pointSize
let font = Font.custom(fontName, size: size, relativeTo: style)
if let weight { return font.weight(weight) }
return style == .headline ? font.weight(.semibold) : font
}
func fixedFont(size: CGFloat) -> Font {
guard let fontName else { return .system(size: size) }
return .custom(fontName, fixedSize: size)
}
func uiFont(for style: UIFont.TextStyle, traits: UITraitCollection) -> UIFont {
let system = UIFont.preferredFont(forTextStyle: style, compatibleWith: traits)
guard let fontName, let custom = UIFont(name: fontName, size: system.pointSize) else { return system }
return custom
}
}
extension Font.TextStyle {
fileprivate var uiStyle: UIFont.TextStyle {
switch self {
case .largeTitle: .largeTitle
case .title: .title1
case .title2: .title2
case .title3: .title3
case .headline: .headline
case .subheadline: .subheadline
case .callout: .callout
case .footnote: .footnote
case .caption: .caption1
case .caption2: .caption2
default: .body
}
}
}
private struct AppDefaultFontModifier: ViewModifier {
@AppStorage("appFont") private var selection = AppFont.system.rawValue
func body(content: Content) -> some View {
let font = AppFont(rawValue: selection) ?? .system
content.font(font == .system ? nil : font.font(.body))
}
}
private struct AppFontModifier: ViewModifier {
@AppStorage("appFont") private var selection = AppFont.system.rawValue
let style: Font.TextStyle
var weight: Font.Weight?
func body(content: Content) -> some View {
content.font((AppFont(rawValue: selection) ?? .system).font(style, weight: weight))
}
}
extension View {
@ViewBuilder
func appFontIfSet(_ style: Font.TextStyle?) -> some View {
if let style {
appFont(style)
} else {
self
}
}
func appDefaultFont() -> some View {
modifier(AppDefaultFontModifier())
}
func appFont(_ style: Font.TextStyle, weight: Font.Weight? = nil) -> some View {
modifier(AppFontModifier(style: style, weight: weight))
}
}