Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the font and text color of the title of UINavigationBar

I want to be change the font of the title to Avenir, and I want to make it white. Here is my code in the viewDidLoad method:

UINavigationBar.appearance().titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 20)!]
    UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

This code only changes the color of the title, not the font. Additionally, I tried implementing this in the App Delegate File, but that did not work.

Here is what my UINavigationBar looks like:enter image description here

I want the title to have a font of Avenir as well. How do I achieve this?

like image 412
Rehaan Advani Avatar asked Aug 06 '15 03:08

Rehaan Advani


People also ask

How do I change the color of my navigation bar title?

Changing the text color The text color of the navigation bar can be changed using two inbuilt classes: navbar-light: This class will set the color of the text to dark. This is used when using a light background color.

How do I change the font style of the navigation bar title in Swift?

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.


4 Answers

It seems this is needed as well:

self.navigationController?.navigationBar.barStyle       = UIBarStyle.Black // I then set the color using:

self.navigationController?.navigationBar.barTintColor   = UIColor(red: 204/255, green: 47/255, blue: 40/255, alpha: 1.0) // a lovely red

self.navigationController?.navigationBar.tintColor = UIColor.whiteColor() // for titles, buttons, etc.

let navigationTitleFont = UIFont(name: "Avenir", size: 20)!

self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: navigationTitleFont]
like image 158
Fred Faust Avatar answered Oct 25 '22 13:10

Fred Faust


If you want the styles to be applied through out the whole app add these lines in the AppDelegate.m file.

NSShadow* shadow = [NSShadow new]; 
shadow.shadowOffset = CGSizeMake(0.0f, 0.0f); 
shadow.shadowColor = [UIColor blackColor]; 

[[UINavigationBar appearance] setTitleTextAttributes: @{
NSForegroundColorAttributeName: [UIColor whiteColor],
NSFontAttributeName: [UIFont fontWithName:@"Kelvetica Nobis" size:20.0f],
NSShadowAttributeName: shadow 
}];

NSForegroundColorAttributeName sets the Font color.

NSFontAttributeName sets a custom font face to the title text.

NSShadowAttributeName applies a nice shadow effect to the text, you can remove this part if you want.

Also in advance, you can add these line to hide the text that comes up in back button in action bar when you navigate to another view within the navigation controller.

[[UIBarButtonItem appearance]
     setBackButtonTitlePositionAdjustment:UIOffsetMake(-1000, -1000)
     forBarMetrics:UIBarMetricsDefault];

Hope this help your question :)

like image 36
Laky Avatar answered Oct 25 '22 11:10

Laky


In case someone needs this in Swift 4:

let navBarAppearance = UINavigationBar.appearance()
navBarAppearance.barTintColor = .red
navBarAppearance.tintColor = .white
navBarAppearance.titleTextAttributes = [
        NSAttributedString.Key.foregroundColor: UIColor.white,
        NSAttributedString.Key.font: UIFont(name: "Avenir", size: 20)!]
like image 29
algrid Avatar answered Oct 25 '22 13:10

algrid


Your code is OK you just need to set all of titleTextAttributes in one line:

UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white , NSFontAttributeName: UIFont(name: "Avenir", size: 17)!]
like image 24
Milad Faridnia Avatar answered Oct 25 '22 12:10

Milad Faridnia