I've got my nav bar mostly customized to my liking, but I'm trying to increase the kerning using NSKernAttributeName
. I'm using the appearance proxy to set the nav bar to white text and a custom font, but when I try to add kerning it doesn't take effect.
[[UINavigationBar appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIColor whiteColor], NSForegroundColorAttributeName,
[UIFont fontWithName:@"HelveticaNeue-Light" size:20.0], NSFontAttributeName,
[NSNumber numberWithFloat:2.0], NSKernAttributeName, nil]];
Do I need to do something else to add some of the less common attributes like kerning to the title label?
I've tried many different ways to accomplish this and found that you can only change the font, text color, text shadow color, and text shadow offset of UINavigationBar as @Jesús A. Alvarez above have said.
I've converted the code in Swift and it works:
According to the documentation, the
titleTextAttributes
ofUINavigationBar
only lets you specify the font, text color, text shadow color, and text shadow offset.If you want to use other attributes, you can create a
UILabel
with theNSAttributedString
you want, and set it as thetitleView
for your controller'snavigationItem
For example:
Above answer updated for Swift 4
I created a superclass, where I defined this method, which you can call from each subclass you want:
UIViewController Extension
I converted the above answers into a UIViewController extension to tidy it away.
Swift 3
Call the extension function from the viewDidLoad() of your view controller.