60

I have a title in my navigation bar and a i want to change it to custom font. I've found this line of code, but it's for when you have a navigation controller.

self.navigationController?.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "LeagueGothic-Regular", size: 16.0)!, 
                                                             NSForegroundColorAttributeName: UIColor.whiteColor()]

But i don't have any navigation controller. I added navigation bar manually to my view.

enter image description here

enter image description here

how can i change comment font?

1
  • Call titleTextAttributes on the reference to the navigation bar. Commented Sep 11, 2016 at 17:15

11 Answers 11

109

Try this:

Objective-C

[[UINavigationBar appearance] setTitleTextAttributes:attrsDictionary];

Swift 3

self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!]

Swift 4

self.navigationController.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "CaviarDreams", size: 20)!]
Sign up to request clarification or add additional context in comments.

13 Comments

Just to clarify this answer, fontname should be of type UIFont and not just the name of the font as a string.
yes, for example let font = UIFont(name: "Lato-Light.ttf", size: 34). @DavidWilliames
Exactly :) although it would be "Lato-Light" not "Lato-Light.tff" :P
Also note that this answer affects all nav bars, not just this one specific nav bar.
I got this error. Cannot assign value of type 'UIFont?' to type [String: Any]? in swift 3.
|
58

Proper way how to set the font for every view controller in Swift (using Appearance proxy):

Swift 5 (and 4.2)

let attributes = [NSAttributedString.Key.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes

Swift 4

let attributes = [NSAttributedStringKey.font: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes

Swift 3

let attributes = [NSFontAttributeName: UIFont(name: "HelveticaNeue-Light", size: 17)!]
UINavigationBar.appearance().titleTextAttributes = attributes

2 Comments

For iOS 13 & above, use UINavigationBarAppearance(), see stackoverflow.com/a/61712504/4439983
None of them work if your navigationBar is set to prefersLargeTitles = true. if you have that set and you want to change the title font style, use navigationController?.navigationBar.largeTitleTextAttributes = [.font: UIFont(name: "CustomFont"), size: 20), .foregroundColor: UIColor.black]
20

You can do this in Storyboard as well, there is a bug in Xcode 10.1 of doing this here is a trick to overcome this as well.

Step 1 - Choose System from Font

enter image description here

Step 2 - Then again choose Custom and it will show all the fonts.

enter image description here

2 Comments

This workaround worked (or is required) even in in Xcode 10.2.1. Thanks 🙏
It takes effect in the interface builder but the navigation bar does not show the font change when run in simulator.
17

SWIFT 4.x

To Change the Navigation bar title font for both Normal & Large Title above iOS 11.x

let navigation = UINavigationBar.appearance()

let navigationFont = UIFont(name: "Custom_Font_Name", size: 20)
let navigationLargeFont = UIFont(name: "Custom_Font_Name", size: 34) //34 is Large Title size by default

navigation.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationFont!]

if #available(iOS 11, *){
    navigation.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.white, NSAttributedStringKey.font: navigationLargeFont!]
}

Large Title has to be set true in Navigation bar.

Comments

7

Swift 5

I will show you how we do it in our company projects.

  1. We create a UINavigationController subclass.
  2. Write our customization code in viewDidLoad.
  3. Make every navigation controller in our storyboard files inherit from it.

That is very good for more than one reason. one of the reasons is the center point of change. Once we change something in the class, all navigation controllers listen to it.

That's how our UINavigationController subclass looks.

COPY PASTE from work project.

import UIKit

class NavigationController: UINavigationController
{
    // MARK: Navigation Controller Life Cycle

    override func viewDidLoad()
    {
       super.viewDidLoad()
       setFont()
    }

    // MARK: Methods

    func setFont()
    {
       // set font for title
       self.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Al-Jazeera-Arabic", size: 20)!]
    
       // set font for navigation bar buttons
       UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Al-Jazeera-Arabic", size: 15)!], for: UIControl.State.normal)
    }
}

Comments

5

Swift 5 Easy way

//Programatically
self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica Neue", size: 40)!]

Physically

enter image description here

enter image description here

enter image description here

enter image description here

Comments

5

Swift 5

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "American Typewriter Bold", size: 36), size: 36)]

Comments

1

To call titleTextAttributes on the reference to the navigation bar use:

let attributes = [NSAttributedStringKey.font: UIFont(name: "Helvetica", size: 17)!]
self.navigationController?.navigationBar.titleTextAttributes = attributes

Comments

1
 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Lato-Semibold", size: 17)!,NSAttributedStringKey.foregroundColor : UIColor.white]

1 Comment

While this code snippet may solve the question, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations!
1

Swift 4.2 Xcode 10

self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Sacramento-Regular", size: 19)!]

Comments

-1

Be carefull, if you use custom font don't write

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "SF-Pro-Display-Medium", size: 18)!], for: UIControl.State.normal)

use font name without sumbol "-"

like this:

UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "SF Pro Display Medium", size: 18)!], for: UIControl.State.normal)

Comments

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.