问题
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?
回答1:
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)
回答2:
Swift 4.2
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "FontName", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "FontName", size: 10)!], for: .selected)
Swift 4
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "FontName", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "FontName", size: 10)!], for: .selected)
Swift 3
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected)
Note: Use setTitleTextAttributes
for both .normal
and .selected
to have changes persist selection state changes.
回答3:
Put this under didFinishLaunchingWithOptions
:
UITabBarItem.appearance()
.setTitleTextAttributes(
[NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!],
for: .normal)
This works in Swift 4
回答4:
In addition @Mc.Lover 's answer, If you want to aplly this change to all of your tab bar Items in application, I recommand to add the code in application
function of AppDelegate class:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//Just add this line to get it done.
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "IranSansMobile", size: 15)!], for: UIControlState.normal)
return true
}
回答5:
In Swift4 version, you can use attribute keys to set font and foreground color
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(hexString: "#D8FFE8"), NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Bold", size: 16) as Any], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor: UIColor(hexString: "#FFFFFF"),NSAttributedStringKey.font : UIFont(name: "HelveticaNeue-Bold", size: 16) as Any], for: .selected)
回答6:
Swift 4.2
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "BahijTheSansArabic-Plain", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "BahijTheSansArabic-Plain", size: 10)!], for: .selected)
回答7:
Swift 5
let appearance = UITabBarItem.appearance()
let attributes = [NSAttributedString.Key.font:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes as [NSAttributedString.Key : Any], for: .normal)
来源:https://stackoverflow.com/questions/26069334/changing-tab-bar-font-in-swift