ios – How to set the default font at the app level for SwiftUI


I have written a home budgeting app that works for me on my iPad, which is where I originally intended it to work. I’m now thinking I can expand the app to the iPhone and learn how to sync the database between the two.

It seems that the biggest obstacle I face is my use of the “title2” font on many (most) of my pages. I’ve experimented and found that on the iPhone if I use “subheadline” where I’ve used “title2” the pages look good.

Instead of putting case or if logic on every page I’m thinking that I could set the apps default Font at the Main.app level and then remove the specific references to .font() on the pages

I’d like to set the default font to “title2” if the device is iPad and to “subheadline” if the device is iPhone.

I’ve seen how to get the device type

import SwiftUI

@main
struct ManagingYourMoneyiPhoneApp: App {
    
        switch UIDevice.current.userInterfaceIdiom {
        case .pad:
            print("This is an iPad")

        case .phone:
            print("This is an iPhone")
        default:
            print("default")
        }
    }

    var body: some Scene {

        WindowGroup {
            ContentView()
        }
    }
}

But I’m unclear as to how to set the default font based on the device type.

I’ve seen an example

ContentView()
    .environment(\.font, Font.custom("CustomFont", size: 14))

but I can’t seem to figure how to implement this conditionally.

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img