I’ve added customized fonts to my venture and have efficiently been in a position to make use of every font to stylize textual content however solely in a settings menu:
class AppearanceManager: ObservableObject {
@Printed var Font: String = "System" {
didSet {
saveFont()
objectWillChange.ship()
print("Set font to (Font)")
print("Corresponding font title: (fonts[Font])")
}
}
public let fonts = ["System" : "system-default", // unrecognized name defaults to system font
"Courier Prime" : "CourierPrime-Regular",
"JetBrains Mono" : "JetBrainsMono-Regular",
"Roboto" : "RobotoMono-Regular",
"Ubuntu" : "UbuntuMono-Regular",
"Victor" : "VictorMono-Regular",
"Times New Roman" : "TimesNewRomanPSMT",
"Trebuchet" : "TrebuchetMS"
]
init() {
loadFont()
// used for debugging
for household in UIFont.familyNames.sorted() {
let names = UIFont.fontNames(forFamilyName: household)
print("Household: (household) Font names: (names)")
}
}
non-public let fontKey = "FontKey"
non-public func saveFont() {
UserDefaults.normal.set(Font, forKey: fontKey)
}
non-public func loadFont() {
if let savedFont = UserDefaults.normal.worth(forKey: fontKey) as? String {
Font = savedFont
}
}
}
struct SettingsView: View {
@EnvironmentObject var appearanceManager: AppearanceManager
var physique: some View {
Kind {
FontSelectionView()
}
.navigationTitle("Settings")
}
}
struct FontSelectionView: View {
@EnvironmentObject var appearanceManager: AppearanceManager
var physique: some View {
Part(header: Textual content("Fonts")) {
VStack(alignment: .main) {
ForEach(appearanceManager.fonts.keys.sorted(), id: .self) { fontName in
FontButton(title: fontName)
}
.padding(.vertical, 5)
}
}
}
}
struct FontButton: View {
@EnvironmentObject var appearanceManager: AppearanceManager
let title: String
var isSelected: Bool {
appearanceManager.Font == title
}
var physique: some View {
HStack {
Textual content(title)
.font(.customized(appearanceManager.fonts[name] ?? "", dimension: 17, relativeTo: .physique))
Spacer()
Circle()
.body(width: 20, peak: 20)
.foregroundColor(isSelected ? .secondary : .clear)
.overlay(
Circle()
.stroke(Shade.major, lineWidth: 2)
)
}
.foregroundColor(.secondary)
.onTapGesture {
withAnimation {
appearanceManager.Font = title
}
}
}
}
The code above can be utilized to use every font to textual content:
Nevertheless sure fonts (not simply customized fonts I added to my venture, but in addition built-in fonts resembling Trebuchet) aren’t being utilized to textual content components all through my app:
struct PostView: View {
@EnvironmentObject var appearanceManager: AppearanceManager
var physique: some View {
ScrollView {
VStack(alignment: .main, spacing: 16) {
Textual content("Whats up World")
// under solely works for sure fonts
.font(.customized(appearanceManager.Font, dimension: 30, relativeTo: .title))
}
.padding()
}
}
}
I’ve injected the EnvironmentObject to the basis view of my app
@essential
struct MyApp: App {
@StateObject non-public var appearanceManager = AppearanceManager()
var physique: some Scene {
WindowGroup {
ContentView()
.environmentObject(appearanceManager)
}
}
}
I’ve additionally printed all out there fonts and decided that the precise font title is being revealed:
// prints in init() methodology:
...
Household: Occasions New Roman Font names: ["TimesNewRomanPSMT", "TimesNewRomanPS-ItalicMT", "TimesNewRomanPS-BoldMT", "TimesNewRomanPS-BoldItalicMT"]
Household: Trebuchet MS Font names: ["TrebuchetMS", "TrebuchetMS-Italic", "TrebuchetMS-Bold", "Trebuchet-BoldItalic"]
Household: Ubuntu Mono Font names: ["UbuntuMono-Regular"]
Household: Verdana Font names: ["Verdana", "Verdana-Italic", "Verdana-Bold", "Verdana-BoldItalic"]
Household: Victor Mono Font names: ["VictorMono-Regular"]
...
// prints after I set a font:
Set font to Trebuchet
Corresponding font title: Non-compulsory("TrebuchetMS") // Not utilized outdoors of settings menu
Set font to JetBrains Mono
Corresponding font title: Non-compulsory("JetBrainsMono-Common") // works as anticipated
Set font to Roboto
Corresponding font title: Non-compulsory("RobotoMono-Common") // not utilized outdoors of settings menu