如何在iOS标签栏中使用自定义字体

时间:2014-02-19 16:41:35

标签: objective-c ios7

我正在为我的iPhone应用程序的UI使用字体真棒资源: FontAwesome

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

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

但现在我想在我的标签栏控制器的标签栏项目中使用它,即我想将标签栏的图标设置为字体真棒元素。怎么办呢?

7 个答案:

答案 0 :(得分:30)

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

答案 1 :(得分:9)

Swift版本:

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

答案 2 :(得分:6)

Swift 3

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

答案 3 :(得分:4)

快捷键4

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

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

答案 4 :(得分:3)

I was not able to change the font using the accepted answer. This is what I used later in my

didFinishLaunchingWithOptions
method of AppDelegate:

    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];

More information here: http://www.appcoda.com/ios-programming-how-to-customize-tab-bar-background-appearance/

答案 5 :(得分:2)

试试这段代码:

[[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];

答案 6 :(得分:2)

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

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