Search code examples
swiftuitabbaruifont

Changing tab bar font in Swift


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?


Solution

  • 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)