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:
我一直在尝试更改标签栏项目的字体,但是我无法找到任何Swift示例。我知道这就是你在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?
但是我怎么能把它翻译成Swift呢?
5 个解决方案
#1
41
The UITextAttributeFont was deprecated in iOS 7. You should use the NS variant instead:
UITextAttributeFont在iOS 7中已弃用。您应该使用NS变体:
import UIKit
let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)
#2
20
Here is Swift 3.0
solution
这是Swift 3.0解决方案
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected)
#3
5
Put this under didFinishLaunchingWithOptions
:
把它放在didFinishLaunchingWithOptions下:
UITabBarItem.appearance()
.setTitleTextAttributes(
[NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!],
for: .normal)
This works in Swift 4
这适用于Swift 4
#4
3
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:
另外@ Mc.Lover的回答,如果你想对应用程序中的所有标签栏项目进行更改,我建议在AppDelegate类的应用程序功能中添加代码:
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
0
In Swift4 version, you can use attribute keys to set font and foreground color
在Swift4版本中,您可以使用属性键来设置字体和前景色
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)
#1
41
The UITextAttributeFont was deprecated in iOS 7. You should use the NS variant instead:
UITextAttributeFont在iOS 7中已弃用。您应该使用NS变体:
import UIKit
let appearance = UITabBarItem.appearance()
let attributes = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 20)]
appearance.setTitleTextAttributes(attributes, forState: .Normal)
#2
20
Here is Swift 3.0
solution
这是Swift 3.0解决方案
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "Font-Name", size: 10)!], for: .selected)
#3
5
Put this under didFinishLaunchingWithOptions
:
把它放在didFinishLaunchingWithOptions下:
UITabBarItem.appearance()
.setTitleTextAttributes(
[NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!],
for: .normal)
This works in Swift 4
这适用于Swift 4
#4
3
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:
另外@ Mc.Lover的回答,如果你想对应用程序中的所有标签栏项目进行更改,我建议在AppDelegate类的应用程序功能中添加代码:
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
0
In Swift4 version, you can use attribute keys to set font and foreground color
在Swift4版本中,您可以使用属性键来设置字体和前景色
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)