57

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?

Hos Ap
  • 1,168
  • 2
  • 11
  • 24

11 Answers11

106

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)!]
shim
  • 9,289
  • 12
  • 69
  • 108
KAR
  • 3,303
  • 3
  • 27
  • 50
  • 2
    Just to clarify this answer, `fontname` should be of type `UIFont` and not just the name of the font as a string. – David Williames Sep 11 '16 at 17:31
  • 1
    yes, for example let font = UIFont(name: "Lato-Light.ttf", size: 34). @DavidWilliames – KAR Sep 11 '16 at 17:33
  • 2
    Exactly :) although it would be "Lato-Light" not "Lato-Light.tff" :P – David Williames Sep 11 '16 at 17:41
  • Also note that this answer affects all nav bars, not just this one specific nav bar. – rmaddy Sep 11 '16 at 23:57
  • I got this error. Cannot assign value of type 'UIFont?' to type [String: Any]? in swift 3. – May Phyu May 25 '17 at 10:02
  • So you need it in swift 3? – KAR May 25 '17 at 10:03
  • Yes bro. @KAR . I want it in swift 3 – May Phyu May 25 '17 at 10:04
  • Have you tried this, self.navigationController.navigationBar.titleTextAttributes = [ NSFontAttributeName: UIFont(name: "CaviarDreams", size: 20)!] ? – KAR May 25 '17 at 10:06
  • @MayPhyu you welcome. If its helpfull to you than upvote the answer and comment – KAR May 25 '17 at 10:15
  • bro @KAR, I don't want to add that code in each view controller. I would like to add in one place and effect in every view controller. How to do this bro? – May Phyu May 25 '17 at 11:05
  • @DavidWilliames bro, could you help me please https://stackoverflow.com/questions/44304498/how-to-set-font-family-to-uiviewcontroller-title-in-swift ? – May Phyu Jun 01 '17 at 10:47
  • **Swift 4.2** `self.navigationController.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "CaviarDreams", size: 20)!]` – Bogdan Razvan Oct 24 '18 at 08:35
  • The Objective-C and Swift code in this answer is not equivalent. The Objective-C line changes the UINavigationBar via the appearance proxy which changes every navigation bar in the app. The Swift versions set only a single navigation bar. – GingerBreadMane Jan 23 '19 at 18:54
56

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
Marián Černý
  • 15,096
  • 4
  • 70
  • 83
  • 2
    For iOS 13 & above, use `UINavigationBarAppearance()`, see https://stackoverflow.com/a/61712504/4439983 – Noor May 10 '20 at 20:56
  • 1
    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]` – Junsu Kim Oct 24 '21 at 07:07
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

Gagandeep Gambhir
  • 4,225
  • 1
  • 29
  • 34
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.

iSrinivasan27
  • 1,406
  • 1
  • 21
  • 28
6

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)
    }
}
Essam Fahmi
  • 1,920
  • 24
  • 31
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

Shakeel Ahmed
  • 5,361
  • 1
  • 43
  • 34
5

Swift 5

navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(descriptor: UIFontDescriptor(name: "American Typewriter Bold", size: 36), size: 36)]
Gustin
  • 181
  • 2
  • 5
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
Retterdesdialogs
  • 3,180
  • 1
  • 21
  • 42
Vitalik Kizlov
  • 385
  • 5
  • 9
1
 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Lato-Semibold", size: 17)!,NSAttributedStringKey.foregroundColor : UIColor.white]
Davender Verma
  • 503
  • 2
  • 12
  • 5
    While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) 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! – Filnor Aug 22 '18 at 08:01
1

Swift 4.2 Xcode 10

self.navigationController!.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: UIFont(name: "Sacramento-Regular", size: 19)!]
Prashant Gaikwad
  • 3,493
  • 1
  • 24
  • 26
-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)
Stacy
  • 1