Changing tab bar font in Swift

user3746428 picture user3746428 · Sep 27, 2014 · Viewed 35.1k times · Source

I have been trying to change the font for the tab bar items however I haven't been able to find any Swift examples. I know that this is how you change it in Objective-C:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateNormal];

But how can I translate this into Swift?

Answer

AlBlue picture AlBlue · Sep 27, 2014

The UITextAttributeFont was deprecated in iOS 7. You should use the NS variant instead:

import UIKit

let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)