Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set navigation bar font colour and size

I have been trying to set both the font size and the font colour of the text in my navigation bar, however I have been having issues.

Basically what seems to be happening is my two lines of code overwrite each other and I can only get them to work on their own.

Here is my code:

    self.navigationController?.navigationBar.titleTextAttributes = [NSFontAttributeName: UIFont(name: "Avenir", size: 30)]
    self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.whiteColor()]

What do I need to change so that I can change the font size and colour?

like image 547
user3746428 Avatar asked Dec 01 '25 21:12

user3746428


1 Answers

In swift Mandav's excellent answer could be implemented this way:

var attributes = [
    NSForegroundColorAttributeName: UIColor.greenColor(),
    NSFontAttributeName: UIFont(name: "Avenir", size: 30)!
]
self.navigationController?.navigationBar.titleTextAttributes = attributes

Note that the UIFont initializer returns an optional because it could fail to find the font, so an exclamation point is used.

like image 123
Steve Rosenberg Avatar answered Dec 04 '25 12:12

Steve Rosenberg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!