TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Custom font in navigation title and back button?

Forums > SwiftUI

In my app I'm using a custom font everywhere. However, the navigation titles and back buttons are displayed in some default font, which doesn't look good in such a context. Is there a way to change the font of navigation titles and back buttons to a custom one?

2      

without knowing how you have implemtned your navigation and titles it's unlikely that anyone will be able to help...

otherwise you're back here https://developer.apple.com/documentation/swiftui/applying-custom-fonts-to-text/

2      

Well, it is all very basic. And, sorry for not making it clear, my "custom" font isn't something really custom, it's the font that exists on iOS devices from time immemorial. It's just not the default system font. So here is an example of the code:

 var body: some View {
        NavigationView {
            List {
                NavigationLink {
                    AboutView()
                } label: {
                    Text("About the App")
                }

                NavigationLink {
                    GettingStartedView()
                } label: {
                    Text("Getting started")
                }

                NavigationLink {
                    FaqView()
                } label: {
                    Text("FAQ")
                }
            }
            .listStyle(.grouped)
            .navigationTitle("Help")
            .navigationBarTitleDisplayMode(.inline)
            .font(.custom("AmericanTypewriter", size: 19))
        }
    }

When running on the simulator or a device there is a noticeable difference between the font of the navigation title (it's the default system font, also used for the back button) and the rest of the app. I would very much prefer not to have that difference.

2      

To use a custom font for the navigation title and back button in your app, you'll need to follow these steps:

Add the custom font to your Xcode project

Drag and drop the font file (with the extension .ttf or .otf) into your project's file navigator in Xcode. Make sure the font file is included in your app's target. Register the custom font in your app's Info.plist file:

Open the Info.plist file in Xcode. Add a new row and set the key as "Fonts provided by application" (or "UIAppFonts" if you're using an older version of Xcode). Under this key, add an item for each custom font you want to use, specifying the filename of the font file (including the extension).

2      

Hi sundraw,

We have to muddle around in UIKit for this kind of customization unfortunately.

Try including this code in you mainApp.swift file above the var body some view declaration, this should configure your custom font as requested.

init() {
    let appear = UINavigationBarAppearance()

    let atters: [NSAttributedString.Key: Any] = [
        .font: UIFont(name: "AmericanTypewriter", size: 19)!
    ]

    appear.largeTitleTextAttributes = atters
    appear.titleTextAttributes = atters
    UINavigationBar.appearance().standardAppearance = appear
    UINavigationBar.appearance().compactAppearance = appear
    UINavigationBar.appearance().scrollEdgeAppearance = appear
 }

5      

Wonderful, thank you very much! And to make it bold, I simply used "AmericanTypewriter-Bold".

4      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Learn more here

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.