How do I change navigationBar font in Swift?
Asked Answered
V

9

34

This is what I have tried so far, but receiving an error (I have correctly implemented CaviarDreams to the project):

self.navigationController.navigationBar.titleTextAttributes = NSFontAttributeName[UIFont .fontWithName(CaviarDreams.ttf, size: 20)]

Error says: Use of unresolved identifier 'CaviarDreams

Venule answered 19/8, 2014 at 16:12 Comment(0)
L
98

Try this:

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

Edit: Now, UIFont must be unwrapped to be able to be used here.

Swift 5 (+ safe handling of optional UIFont)

self.navigationController?.navigationBar.titleTextAttributes = [ NSAttributedString.Key.font: UIFont(name: "Caviar-Dreams", size: 20) ?? UIFont.systemFont(ofSize: 20)]
Lamarckian answered 19/8, 2014 at 16:30 Comment(5)
Worked like a charm, thanks. Have been fumbling around with this for an hour or so, trying to figure it out myself, Swift you know, kinda new to it. :PVenule
Note: NSFontAttributeName is now of type String so this code doesn't work.Eyot
@Eyot don't understand how to fix this. Do you have suggestions?Abdominous
NSFontAttributeName is still a String. I think Dehli meant that UIFont(...) now returns a UIFont? instead of a UIFont. See UIFont.h for details. benaneesh's answer solves the error relating to these that you'll see when you upgrade to the latest Xcode.Brought
For Swift 4, NSFontAttributeName was renamed. It is titleTextAttributes = [NSAttributedString.Key.font: UIFont.boldSystemFont(ofSize: 23)] now.Joppa
C
44

Using Swift, I added this to AppDelegate.swift in

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.

        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "DINNextLTW04-Regular", size: 20)!
        ]

        return true
    }

Hope it helps!

Countervail answered 20/7, 2015 at 8:33 Comment(4)
Your solution is awesome.Shewchuk
Modeled it after you my friend :)Countervail
Much better approach. Thanks!Archeology
NSFontAttributeName was now changed to --->> NSAttributedStringKey.fontEugenol
C
12

Swift 2.0:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        UINavigationBar.appearance().titleTextAttributes = [
            NSFontAttributeName: UIFont(name: "Arial-Regular", size: 30)!
        ]

        return true
    }

Or

 override func viewDidLoad() {
  super.viewDidLoad()

  self.navigationController?.navigationBarHidden =  false
  self.title = "SAMPLE"

//Set Color
  let attributes: AnyObject = [ NSForegroundColorAttributeName: UIColor.redColor()]
  self.navigationController!.navigationBar.titleTextAttributes = attributes as? [String : AnyObject]


//Set Font Size
  self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Arial", size: 37.0)!];

 }
Cuneate answered 8/3, 2016 at 13:33 Comment(2)
Hi Alvin, when I put in your font size line in viewDidLoad() (xcode 7.3.1), it compiles fine but at (simulator) run time it crashes with EXC_BAD_INSTRUCTION (code=EXC_1386_INVOP, subcode=0x0) with console msg "fatal error: unexpectedly found nil while unwrapping an Optional value". I have the navigation bar in the FirstViewController of a tabbed view template. Might you have any suggestions? ThanksHypnosis
This works now: UINavigationBar.appearance().titleTextAttributes = [ NSForegroundColorAttributeName : UIColor.blueColor(), NSFontAttributeName : UIFont(name: "Bradley Hand", size: 28)! ]Hypnosis
P
10

Now you have to unwrap (!) it first so its not of type UIFont?:

self.navigationController.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "<font-name>", size: <size>)!]
Polygamist answered 14/11, 2014 at 20:54 Comment(0)
E
4

For Swift 2.3

self.navigationController!.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Roboto-Bold", size: 20.0)!, NSForegroundColorAttributeName : UIColor.whiteColor()];
Erewhile answered 22/12, 2016 at 20:47 Comment(0)
F
4

Swift 4

if let font = UIFont(name: "FontName", size: 16) {

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.font: font]

}

Or as the other answer recommended doing it on the AppDelegate:

  if let font = UIFont(name: "FontName", size: 16) {

    UINavigationBar.appearance().titleTextAttributes = [
          NSAttributedStringKey.font: font]

}
Foresaid answered 1/4, 2018 at 7:50 Comment(0)
D
2

Swift 4.2

Change Large Title

self.navigationController!.navigationBar.largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 22)]

Change Small Title when layout scrolled down

self.navigationController!.navigationBar.titleTextAttributes = [.font: UIFont.systemFont(ofSize: 14)]
Durkee answered 30/6, 2019 at 7:6 Comment(0)
T
1

Swift 4.2

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
  // Override point for customization after application launch.

  let font = UIFont(name: "FontName", size: 16) ?? UIFont.systemFont(ofSize: 16)
  UINavigationBar.appearance().titleTextAttributes = [.font: font]
}
Transudation answered 27/9, 2018 at 7:3 Comment(0)
L
1

Swift 4.2

 self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.font: UIFont(name: "Helvetica", size: 18.0)!]
Lapidate answered 10/10, 2018 at 20:21 Comment(1)
You can do just .font instead of NSAttributedString.Key.fontGodman

© 2022 - 2024 — McMap. All rights reserved.