2

I'm currently using a custom font in my app, but am finding it a bit frustrating having to use .font(Font.custom("Montserrat-Regular", size: 16)) everywhere. Is there a way I can set the the entire app to be using this font, and I can simply use .font(.system(size: 16)) and even use standard sizes like .font(.caption) and it will use the custom font?

I've actually set .font(Font.custom("Montserrat-Medium", size: 16)) on my ContentView as shown below, which allows me to use this font everywhere, but the size is always 16, and when I change it, the font goes back to the default font.

@main
struct AppName: App {
  var body: some Scene {
    WindowGroup {
        ContentView()
            .font(Font.custom("Montserrat-Regular", size: 16))
    }
  }
}

1 Answer 1

3

You could create constants using a custom Type, for example AppFont like so:

enum AppFont {

    static let header       = Font.custom(Raleway.extraBold.weight,    size: 40)
    static let header2      = Font.custom(Raleway.extraBold.weight,    size: 30)
    static let title        = Font.custom(Raleway.bold.weight,         size: 32)
    static let title2       = Font.custom(Raleway.extraBold.weight,    size: 22)
    static let subtitle     = Font.custom(Raleway.bold.weight,         size: 16)
    static let body         = Font.custom(Raleway.medium.weight,       size: 17)
    static let body2        = Font.custom(Raleway.regular.weight,      size: 17)
    static let footnote     = Font.custom(Raleway.regular.weight,      size: 16)
    static let footnote2    = Font.custom(Raleway.regular.weight,      size: 13)
    
    static var custom: (Raleway, CGFloat) -> Font = { (weight, size) in
        return Font.custom(weight.weight, size: size)
    }
}

enum Raleway: String {

    case black        = "Raleway-Black"
    case extraBold    = "Raleway-ExtraBold"
    case bold         = "Raleway-Bold"
    case semiBold     = "Raleway-SemiBold"
    case medium       = "Raleway-Medium"
    case regular      = "Raleway-Regular"
    case light        = "Raleway-Light"
    case extraLight   = "Raleway-ExtraLight"
    case thin         = "Raleway-Thin"
    
    var weight: String { return self.rawValue }
}

Usage

Text("Example")
    .font(AppFont.header)

This is an excerpt from my app, you may replace Raleway with your font, namely Montserrat or any font. Just to give you an example.

Note

I would not recommend simply extending Font. I find it more clear to have custom Types for any app-custom stuff.

Sign up to request clarification or add additional context in comments.

2 Comments

This is absolutely perfect. Thanks!
Still, the original question is somewhat open. I’d like to see the built in headline, body, footnote, and the like to use a custom font without having to explicitly state it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.