【发布时间】:2016-08-12 05:39:50
【问题描述】:
我想知道如何在使用标签栏时更改标签中标题的字体和大小。
我查看了文档,但找不到任何关于标题字体和大小的信息 - source
【问题讨论】:
标签: ios swift fonts uitabbarcontroller
我想知道如何在使用标签栏时更改标签中标题的字体和大小。
我查看了文档,但找不到任何关于标题字体和大小的信息 - source
【问题讨论】:
标签: ios swift fonts uitabbarcontroller
您可以通过外观代理更改它:
let font: UIFont = ...
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: font], forState: .Normal)
斯威夫特 4:
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: font], for: .normal)
你应该把它放在你的应用代理中func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
【讨论】:
.Normal 状态将其设置为正常,其他状态将回退到此值。
Swift 3 的更新。
把它放在你的应用代理中func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: yourFont], for: .normal)
【讨论】:
斯威夫特 5.5
let font: UIFont = UIFont(font: "arial", size: 15)! UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: 字体],用于:.normal)
【讨论】:
斯威夫特 4.1
UITabBarItem.appearance().setTitleTextAttributes([kCTFontAttributeName as NSAttributedStringKey: font], for: .normal)
【讨论】:
我发现这个 Swift 5 解决方案很有用:
UITabBarItem.appearance().setTitleTextAttributes([.font: UIFont(name: "FontName", size: 10)!], for: .normal)
【讨论】:
在我的情况下,这个解决方案对我有用(Swift 5.5):
let fontSize: CGFloat = 12
if #available(iOS 13, *) {
let appearance = tabBarController.tabBar.standardAppearance
appearance.stackedLayoutAppearance.normal.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
]
appearance.stackedLayoutAppearance.selected.titleTextAttributes = [
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
]
} else {
if #available(iOS 11, *) {
UITabBarItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([
NSAttributedString.Key.font: UIFont.systemFont(ofSize: fontSize, weight: .medium)
], for: .selected)
}
}
【讨论】: