iOS Development

ios – Sure fonts not making use of to particular views

Spread the love


I’ve added customized fonts to my venture and have efficiently been ready to make use of every font to stylize textual content however solely in a settings menu:

class AppearanceManager: ObservableObject {

    @Revealed 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)")
        }
    }
    
    personal let fontKey = "FontKey"
    
    personal func saveFont() {
        UserDefaults.customary.set(Font, forKey: fontKey)
    }
    
    personal func loadFont() {
        if let savedFont = UserDefaults.customary.worth(forKey: fontKey) as? String {
            Font = savedFont
        }
    }
}

struct SettingsView: View {
    @EnvironmentObject var appearanceManager: AppearanceManager
    
    var physique: some View {
        Type {
            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, top: 20)
                .foregroundColor(isSelected ? .secondary : .clear)
                .overlay(
                    Circle()
                        .stroke(Colour.main, lineWidth: 2)
                )
        }
        .foregroundColor(.secondary)
        .onTapGesture {
            withAnimation {
                appearanceManager.Font = title
            }
        }
    }
}

The code above can be utilized to use every font to textual content:
enter image description here

Nevertheless sure fonts (not simply customized fonts I added to my venture, but additionally built-in fonts corresponding to Trebuchet) are usually not 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("Good day World")
                    // under solely works for sure fonts
                    .font(.customized(appearanceManager.Font, dimension: 30, relativeTo: .title))

            }
            .padding()
        }
    }
}

I’ve injected the EnvironmentObject to the foundation view of my app

@essential
struct MyApp: App {
    
    @StateObject personal 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: Instances 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 exterior 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 exterior of settings menu

Leave a Reply

Your email address will not be published. Required fields are marked *