3

I am trying to change the font for the Navigation View title to the system rounded one, but I can't seem to find a way to do it.

To be clear, I can change the font fine on any Text View, List etc, but on the Navigation Title it seems to be impossible.

For reference the title ("Settings") in the app Carrot Weather achieves this: enter image description here

Any idea?

Thanks !!!

  • Check this out: https://stackoverflow.com/a/57631230/14351818 – aheze Jul 17 '21 at 01:50
  • I've seen it, but the [.font : UIFont(name: "Georgia-Bold", size: 20)!] doesn't take the system(.largetitle, design: .rounded) attribute, hence I can't get it to work with system font or even a custom font =/ – Charlie Maréchal Jul 17 '21 at 01:53
  • Does this answer your question https://stackoverflow.com/a/64304302/12299030? – Asperi Jul 17 '21 at 06:37

1 Answers1

8

You can use largeTitle to get the default navigation bar font, then apply rounding using withDesign(_:).

struct ContentView: View {
    init() {
        var titleFont = UIFont.preferredFont(forTextStyle: .largeTitle) /// the default large title font
        titleFont = UIFont(
            descriptor:
                titleFont.fontDescriptor
                .withDesign(.rounded)? /// make rounded
                .withSymbolicTraits(.traitBold) /// make bold
                ??
                titleFont.fontDescriptor, /// return the normal title if customization failed
            size: titleFont.pointSize
        )
        
        /// set the rounded font
        UINavigationBar.appearance().largeTitleTextAttributes = [.font: titleFont]
    }
    var body: some View {
        NavigationView {
            Text("Hello World!")
                .navigationTitle("Dashboard") /// use this for iOS 14+
        }
    }
}

Result:

Rounded navigation bar title
aheze
  • 24,434
  • 8
  • 68
  • 125
  • Is there a way to dynamically update this font? So that if a user changes an in app setting for the font they want to use as the 'theme', that we can then update the textAttributes? – Anthony Oct 11 '22 at 05:16