How to set the color and font size of the navigation bar

I am trying to set both the font size and font color of the text in my navbar, however I was having problems.

Basically, it looks like my two lines of code are overwriting each other and I can 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 to change the font size and color?


source to share

2 answers

In swift Mandawa, a great answer can be implemented like this:

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


Note that the initializer UIFont

returns an optional parameter as it may not find the font, so an exclamation mark is used.



I came across this thread, but I needed an answer for Objective-c. I found out how to do it. Let me post it here in case another programmer encounters the same problem. For objective-c, you can achieve this by setting attributes in NSDictionary like this.

[self.navigationController.navigationBar setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor]                                                ,NSForegroundColorAttributeName,                                                       [UIFont fontWithName:@"Avenir" size:30]                                                       , NSFontAttributeName, nil]];




All Articles