change navigation bar title font - swift
IosSwiftNavigationbarIos 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.
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
Step 2 - Then again choose Custom and it will show all the fonts.
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
Solution 6 - Ios
Swift 5
I will show you how we do it in our company projects.
- We create a
UINavigationController
subclass. - Write our customization code in
viewDidLoad
. - 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)