【问题标题】:Changing font in UITabBarItem更改 UITabBarItem 中的字体
【发布时间】:2012-06-19 15:08:57
【问题描述】:

您好,我有这段代码,但它不起作用,我做错了什么?

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

顺便说一句,这不是我 viewDidLoad 中唯一的东西,但我只是想向你们展示我放它的地方。

【问题讨论】:

  • 你能分享一下你的目标是什么版本的iOS吗?此功能是 iOS 5 的新功能。
  • @ctrahey 我的目标是 iOS 5。

标签: iphone ios xcode uitabbarcontroller


【解决方案1】:

根据: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];

【讨论】:

  • 更改状态:UIControlStateNormal
  • 此处未明确提及。您可以将此代码放在应用委托中的 didFinishLaunchingWithOptions 函数中,以便为应用设置它
  • 这对我很有效: UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name: "AmericanTypewriter", size: 20.0)] as [NSObject : AnyObject!], forState: UIControlState .正常)
  • NSFontAttributeName for iOS7+, UITextAttributeFont for iOS6-
  • @{NSFontAttributeName: [UIFont fontWithName:@"AmericanTypewriter" size:20.0f]} 2015 年。
【解决方案2】:

快捷方式,适合懒人:

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

【讨论】:

【解决方案3】:

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)

【讨论】:

    【解决方案4】:

    斯威夫特 3

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

    【讨论】:

      【解决方案5】:

      斯威夫特 4

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

      【讨论】:

        【解决方案6】:

        如果我在 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:这也适用于自定义字体。

        【讨论】:

          猜你喜欢
          • 2019-06-13
          • 2017-11-12
          • 2012-04-06
          • 1970-01-01
          • 2011-03-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多