So i have this code that should change the nav bar title font, but it doenst
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:[UIFon
Anyone needs a Swift 3 version. redColor()
has changed to just red
.
self.navigationController?.navigationBar.titleTextAttributes =
[NSForegroundColorAttributeName: UIColor.red,
NSFontAttributeName: UIFont(name: "{your-font-name}", size: 21)!]
I had one problem because when I tried to change my NavigationItem title programmatically i was not able to find my family font (tried many things but impossible to make it run correctly) so I found one workaround very nice and easy in storyboard.
Finally you should have something like that in document outline:
And something like that in your ViewController:
Hope it can help.
Don't forget to add the Raw values of the keys to avoid compile errors.
let textAttributes:[NSAttributedStringKey: Any] = [NSAttributedStringKey(rawValue: NSAttributedStringKey.foregroundColor.rawValue):UIColor.blue, NSAttributedStringKey(rawValue: NSAttributedStringKey.font.rawValue):UIFont(name:"OpenSans", size: 17)!]
navigationController?.navigationBar.titleTextAttributes = textAttributes
Swift 4.2
self.navigationController?.navigationBar.titleTextAttributes =
[NSAttributedString.Key.foregroundColor: UIColor.white,
NSAttributedString.Key.font: UIFont(name: "LemonMilklight", size: 21)!]
ADD this single line code in your App Delegate - Did Finish Lauch. It will change Font, color of navigation bar throughout the application.
UINavigationBar.appearance().titleTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.white, NSAttributedString.Key.font: UIFont(name: "YOUR FONT NAME", size: 25.0)!]
This one for alternative to Swift 4 (already answer by @Josh):
let titleTextAttributed: [NSAttributedStringKey: Any] = [.foregroundColor: UIColor.red, .font: UIFont(name: "AvenirNext-Regular", size: 20) as Any]
navigationController?.navigationBar.titleTextAttributes = titleTextAttributed