xcode 更改UITabBarItem中的字体

5jdjgkvh  于 2023-01-21  发布在  其他
关注(0)|答案(7)|浏览(119)

嗨,我有这个代码,它不工作,我做错了什么?

- (void)viewDidLoad
{    
    [self.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"AmericanTypewriter" size:20.0f], UITextAttributeFont, nil] forState:UIControlStateDisabled];
}

顺便说一句,这不是我的视图中唯一的东西,但我只是想告诉你们,我把它放在那里。

13z8s7eq

13z8s7eq1#

依据:How to change the Color of text in UITabBarItem in iOS 5
看起来解决方案可能将消息发送到外观代理,而不是一个项目:

(在iOS 7.0以上版本中已弃用)

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];

适用于iOS 7.0以上用途:

[[UITabBarItem appearance] setTitleTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} forState:UIControlStateNormal];
ttvkxqim

ttvkxqim2#

快捷的方式,为懒惰:

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFontOfSize(10)], forState: .selected)
twh00eeo

twh00eeo3#

Swift 4.1和自定义字体

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Montserrat-Medium", size: 11)], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont(name: "Montserrat-Medium", size: 11)], for: .selected)
uujelgoq

uujelgoq4#

雨燕3

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "OpenSans", size: 10)!], for: .normal)
jmo0nnb3

jmo0nnb35#

雨燕4

UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: UIFont.tabbar], for: .normal)
ppcbkaq5

ppcbkaq56#

如果我在viewDidLoad()中添加了代码,那么当选项卡栏被选中时,我永远无法更改字体。
这是一个伟大的文章,解释了如何做到这一点与更多的细节:HolySwift Article
简而言之,您需要在标签栏控制器中添加以下代码:

override var selectedIndex: Int { 
    didSet {
        guard let selectedViewController = viewControllers?[selectedIndex] else {
            return
        }
        selectedViewController.tabBarItem.setTitleTextAttributes([.font: UIFont.boldSystemFont(ofSize: 13)], for: .normal) 
    }
}

还有这个

override var selectedViewController: UIViewController? { 
    didSet {

        guard let viewControllers = viewControllers else {
            return
        }

        for viewController in viewControllers {
            if viewController == selectedViewController {
                viewController.tabBarItem.setTitleTextAttributes([.font: UIFont.boldSystemFont(ofSize: 13)], for: .normal)
            } else {
                viewController.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 12)], for: .normal)
            }
        }
    }
}

PS:这也可以用自定义字体。

eit6fx6z

eit6fx6z7#

雨燕5

假设您有一个继承UITabBarController的类,如下所示:

final class YourTabBarController: UITabBarController { .. }

在此YourTabBarController中定义2种类型的属性:

let normalTabBarAttributes: [NSAttributedString.Key: Any] = [
    .font: NotSelectedFont,
    .foregroundColor: NotSelectedColor
]
let selectedTabBarAttributes: [NSAttributedString.Key: Any] = [
    .font: SelectedFont,
    .foregroundColor: SelectedColor
]

您有一个名为YourViewControllerViewController,它包含TabBar并继承UIViewController,如下所示:

final class YourViewController: UIViewController {...}

iOS 15.0或更高版本

YourViewController中定义方法:

private func setTabBarAttributes() {
    guard let yourTabBarController = tabBarController as? YourTabBarController else {
        return
    }
    let appearance = UITabBarAppearance()
    appearance.stackedLayoutAppearance.normal.titleTextAttributes = yourTabBarController.normalTabBarAttributes
    appearance.stackedLayoutAppearance.selected.titleTextAttributes = yourTabBarController.selectedTabBarAttributes
    yourTabBarController.tabBar.standardAppearance = appearance
    yourTabBarController.tabBar.scrollEdgeAppearance = appearance
}

YourViewControlleroverride func viewDidLoad()方法调用该方法:

setTabBarAttributes()

iOS 15.0之前的版本

YourTabBarController中,您可以定义一个方法:

private func updateTabBarAttributes() {
    guard let viewControllers = viewControllers else { return }
    for viewController in viewControllers {
        if viewController == selectedViewController {
            viewController.tabBarItem.setTitleTextAttributes(selectedTabBarAttributes, for: .normal)
        } else {
            viewController.tabBarItem.setTitleTextAttributes(normalTabBarAttributes, for: .normal)
        }
    }
}

现在使用如下方法:

override var selectedIndex: Int {
    didSet {
        updateTabBarAttributes()
    }
}

override var selectedViewController: UIViewController? {
    didSet {
        updateTabBarAttributes()
    }
}

相关问题