swiftuifonts

Is it possible to override the default font in a SwiftUI app?


I'm building a SwiftUI app that has a custom font. There are many articles on how to do that and all of them requires that I set some kind of modifier on every single text.

This can't be real, that Apple prevents us to override the default font used by adding a text:

Text("This is a simple text, using the default global font.")

There is the built-in modifier:

Text("...")
   .font(.custom("FontName", size: 13)

There is the environment identifier, which allows to se the default text on an entire view:

.environment(\.font, Font.custom("RobotoMono-Regular", size: 14))

But dear god, why do I have to do this on every single view?

And none of the above allows me to utilize the built-in styling of:

Text("Text")
   .font(.title)

AND none of the above - or the many hacks on the internet allows changing the font in the navigation top bar or tab bar...

What am I missing here? How do I do that?


Solution

  • Here are a few techniques that might help:

    ContentView()
        .font(.custom("AmericanTypewriter", size: 16))
    
    extension Font {
        static let customTitle = Font.custom("AmericanTypewriter", size: 28)
    }
    
    Text("hello").font(.customTitle)