change navigation bar title font - swift

IosSwiftNavigationbar

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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)!]

Solution 2 - Ios

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

Solution 3 - Ios

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

Solution 4 - Ios

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.

Solution 5 - Ios

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

Solution 6 - Ios

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)
    }
}

Solution 7 - Ios

Swift 5

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

Solution 8 - Ios

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

Solution 9 - Ios

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

Solution 10 - Ios

Swift 4.2 Xcode 10

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

Solution 11 - Ios

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHos ApView Question on Stackoverflow
Solution 1 - IosKARView Answer on Stackoverflow
Solution 2 - IosMarián ČernýView Answer on Stackoverflow
Solution 3 - IosGagandeep GambhirView Answer on Stackoverflow
Solution 4 - IosiSrinivasan27View Answer on Stackoverflow
Solution 5 - IosShakeel AhmedView Answer on Stackoverflow
Solution 6 - IosEssam FahmiView Answer on Stackoverflow
Solution 7 - IosGustinView Answer on Stackoverflow
Solution 8 - IosVitalik KizlovView Answer on Stackoverflow
Solution 9 - IosDavender VermaView Answer on Stackoverflow
Solution 10 - IosPrashant GaikwadView Answer on Stackoverflow
Solution 11 - IosStacyView Answer on Stackoverflow