在Swift中更改选项卡栏字体

39

我一直在尝试更改选项卡栏目的字体,但是我无法找到任何Swift示例。 我知道这是如何在Objective-C中更改它的:

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

但是我该如何将这个翻译成Swift呢?

8个回答

68

iOS 7中已弃用UITextAttributeFont,您应改用NS变体:

import UIKit

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

没关系,我把代码放在awakeFromNib里面了,问题解决了。非常感谢! - user3746428
注意:使用 Playground 是发现这种问题的好方法。您还可以访问 http://swifter.natecook.com 查找一些名称。 - AlBlue
2
截至XCode 6.1,这似乎不起作用。我得到了一个“NSString”与“NSObject”不相同的错误。你们也遇到过这种情况吗? - Andres C
18
从6.1版本开始,您必须拆开字体。只需在字体初始化后加上!。 - WCByrne
2
这对于.selected状态不改变我的字体。 - AydinAngouti
显示剩余2条评论

61

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)

注意: 使用 setTitleTextAttributes 方法时需要同时针对 .normal.selected 两种状态进行设置,以确保更改在选择状态变化后仍然有效。


10
这不会改变我在.selected状态下的字体。 - AydinAngouti
@AbbasAngouti- 你只需要复制代码并将 .normal 更改为 .selected 即可。 - Kevin Jantzer
我应该把这段代码放在哪里?我应该为TabBarController创建一个类吗? - bibscy
1
可以确认它对所选内容不起作用。 - Peter Lapisu

11

将以下代码放在didFinishLaunchingWithOptions方法中:

UITabBarItem.appearance()
    .setTitleTextAttributes(
        [NSAttributedStringKey.font: UIFont(name: "Didot", size: 10)!], 
    for: .normal)

这适用于Swift 4


对我来说,在UITabBarItem.appearance()上调用此方法没有起作用。不得不将其替换为self.navigationItem.rightBarButtonItem。 - Marcin D

8

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)

4
除了@Mc.Lover的回答外,如果您想将此更改应用于应用程序中的所有标签栏项目,我建议将代码添加到AppDelegate类的application函数中:
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
}

4

还有一个答案,但针对的是:

if #available(iOS 15, *) {
    let tabBarAppearance = UITabBarAppearance()
    tabBarAppearance.backgroundColor = backgroundColor
    tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [.foregroundColor: selectedItemTextColor, .font: UIFont(name: "American Typewriter", size: 20)]
    tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [.foregroundColor: unselectedItemTextColor, .font: UIFont(name: "American Typewriter", size: 20)]
    tabBar.standardAppearance = tabBarAppearance
    tabBar.scrollEdgeAppearance = tabBarAppearance
}

3

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)

在AppDelegate.swift文件中的application函数内,具体为application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { }。 - Ahmed Safadi

2
在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)

无法在所选中运行 - Peter Lapisu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接