3

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?

user3746428
  • 11,047
  • 20
  • 81
  • 137

2 Answers2

12

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.

Jack Lawrence
  • 10,664
  • 1
  • 47
  • 61
Steve Rosenberg
  • 19,348
  • 7
  • 46
  • 53
0

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

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