【问题标题】:How to use a custom font in iOS tab bar如何在 iOS 标签栏中使用自定义字体
【发布时间】:2014-02-19 16:41:34
【问题描述】:

我正在为我的 iPhone 应用程序的 UI 使用 font awesome 资源: FontAwesome

我在我的应用程序屏幕中使用了它,如下所示:

Phone.font = [UIFont fontWithName:kFontAwesomeFamilyName size:40];
Phone.text = [NSString fontAwesomeIconStringForIconIdentifier:@"fa-phone"];

但是现在我想在我的标签栏控制器的标签栏项目中使用它,即我想将标签栏的图标设置为字体很棒的元素。如何做到这一点?

【问题讨论】:

  • 这个问题已经解决了吗?

标签: objective-c ios7


【解决方案1】:

根据:How to change the Color of text in UITabBarItem in iOS 5

看起来解决方案可能是将消息发送到外观代理,而不是一个项目:

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

这里有一些参考

how-to-change-the-color-of-text-in-uitabbaritem-in-ios-5

ios5-tabbar-fonts-and-color

【讨论】:

    【解决方案2】:

    Swift 版本:

        UITabBarItem.appearance().setTitleTextAttributes(
                [NSFontAttributeName: UIFont(name:"Ubuntu", size:11)!, 
                    NSForegroundColorAttributeName: UIColor(rgb: 0x929292)], 
                forState: .Normal)
    

    【讨论】:

    • 从 6.1 开始,您必须解开字体。放!在你的字体初始化之后
    • 谢谢,是的,它是从Swift 1.2 开始的。已编辑。
    【解决方案3】:

    斯威夫特 3

    UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont(name:"Latio-Regular", size:14)!, NSForegroundColorAttributeName: UIColor.white], for: .normal)
    

    【讨论】:

      【解决方案4】:

      斯威夫特 4

      这是一个如何与 Swift 4 配合使用的示例:

      UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont(name: "Renner-it-Book", size: 10)!, NSAttributedStringKey.foregroundColor: UIColor.gray], for: .normal)
      

      【讨论】:

        【解决方案5】:

        我无法使用接受的答案更改字体。这是我后来在我的

        中使用的 AppDelegate 的
        didFinishLaunchingWithOptions
        方法:
         UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
            UITabBar *tabBar = tabBarController.tabBar;
            UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
            UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
        
            [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateSelected];
        
            [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateSelected];
        
            [tabBarItem1 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateNormal];
        
            [tabBarItem2 setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17 weight:UIFontWeightBold], NSFontAttributeName, nil] forState:UIControlStateNormal];

        更多信息在这里:http://www.appcoda.com/ios-programming-how-to-customize-tab-bar-background-appearance/

        【讨论】:

          【解决方案6】:

          试试这个代码:

          [[UITabBarItem appearance] setTitleTextAttributes: @{NSForegroundColorAttributeName: TAB_BAR_TEXT_NORMAL_COLOR, NSFontAttributeName: [UIFont fontWithName:CUSTOM_FONT_NAME size:TAB_BAR_FONT_SIZE]} forState:UIControlStateNormal];
          [[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColor whiteColor], NSFontAttributeName: [UIFont fontWithName:CUSTOM_FONT_NAME size:TAB_BAR_FONT_SIZE]} forState:UIControlStateSelected];
          

          【讨论】:

            【解决方案7】:

            由于在 iOS7 中 UITextAttributeFont 已弃用,请使用 NSFontAttributeName

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

            【讨论】:

              【解决方案8】:

              使用它可以完美运行...

              UIView *iv = [[UIView alloc] initWithFrame:CGRectMake(0,0,230.0,80.0)];
              UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 230, 80)];
              lbl.textColor = [UIColor whiteColor];
              lbl.font = [UIFont fontWithName:@"Ubuntu" size:18.0f];
              lbl.textAlignment = NSTextAlignmentCenter;
              lbl.text = [NSString stringWithFormat:@"%lu Store Found",(unsigned long)arrOfferList.count];
              [iv addSubview:lbl];
              self.tabBarController.navigationItem.titleView = iv;
              

              【讨论】:

                猜你喜欢
                • 2011-09-06
                • 2012-05-07
                • 1970-01-01
                • 2020-03-14
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-12-27
                相关资源
                最近更新 更多