Search code examples
iosswiftfontsswiftuiuinavigationbar

Navigation bar title does not remain bold upon font change


I changed my navigation bar title to the Apple's New York serif typeface using this code (based on this answer).

init() {
        // if NavigationBarTitle is with Large Font
        if let newYorkDescriptor = UIFontDescriptor.preferredFontDescriptor(withTextStyle: .largeTitle).withDesign(.serif) {
            UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(descriptor: newYorkDescriptor, size: 0)]
        }
    }

The navigation bar title shows up with the right font and font size, but it loses the bolded look that the normal navigation bar title has. How can I regain that? Is there a bold or font weight parameter I'm missing?


Solution

  • The bold is part of font descriptor and you have to add it explicitly, like

    UIFontDescriptor.preferredFontDescriptor(withTextStyle: .largeTitle)
         .withDesign(.serif)?.withSymbolicTraits(.traitBold)