如何在iOS选项卡栏中使用自定义字体

16

我在我的iPhone应用程序的用户界面中使用了Font Awesome资源:FontAwesome

我已经在我的应用程序屏幕中像下面这样使用它:

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

但是现在我想在我的选项卡控制器的选项卡栏项目中使用它,即我想将选项卡栏的图标设置为字体 awesome 元素。如何实现?


这个问题已经解决了吗? - headkit
8个回答

30

9

Swift版本:

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

从6.1版本开始,您必须取消字体的包装。只需在字体初始化后加上!即可。 - hash3r
谢谢,是的,它始于Swift 1.2。已编辑。 - superarts.org

7

Swift 3

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

4

Swift 4

以下是使用Swift 4的示例:

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

3

我无法使用被接受的答案更改字体。这是我稍后在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/


2

自iOS7起,UITextAttributeFont已被弃用,请使用NSFontAttributeName

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

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

1
使用这个,它可以完美地工作...
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;

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