swiftuinavigationbarnavigationbarpreferslargetitles

Changing the font of a Large Titled Navigation Bar in Swift


How do I change the font of a Large Titled Navigation Bar in Swift?

I was unable to find a thread which covered implementing custom fonts for a navigation bar with a large title.

I am trying to use the font titled: "ヒラギノ角ゴシック W8.ttc". This appears to be pre-installed on my Mac. The name in the Font Book is: "Hiragino Kaku Gothic StdN".

This is what I have tried:

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "ヒラギノ角ゴシック-W8", size: 20)!]

and

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "ヒラギノ角ゴシックW8", size: 20)!]

and

self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Hiragino Kaku Gothic StdN", size: 20)!]

Font The "W8" is the Bold version of this font.

Is the problem that the font file name is in a different language or is it something else?


Solution

  • Please check the images how to add font in <code>info.plist</code>

    Fist make sure that your font is .ttf or .otf format

    1) Import your font into the project 2) Add new key "Fonts provided by application" on application's info.plist file and add your font names Now you can use custom fonts with interface builder or programatically

    navigationController?.navigationBar.largeTitleTextAttributes =
        [NSAttributedStringKey.foregroundColor: UIColor.blue,
         NSAttributedStringKey.font: UIFont(name: "your font name", size: 30) ??
                                     UIFont.systemFont(ofSize: 30)]