如何在Objective-C中设置选项卡标题的字体?

5

我想将tabBar标题的字体设置为ubuntu,但我无法设置。

我编写了以下代码:

    self.tabBarController.navigationController.navigationBar.titleTextAttributes = 
      @{NSForegroundColorAttributeName: [UIColor whiteColor], 
      NSFontAttributeName:[UIFont fontWithName:@"Ubuntu" size:9.0f]};

检查字体是否已正确添加,否则请尝试添加另一种字体并再次检查。 - Anbu.Karthik
是的,字体已正确添加。 - Harshit
2个回答

1

Objective C 代码-

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

Swift 代码 -

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

更多参考资料- iOS5 TabBar 字体和颜色


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 提供, 点击上面的
可以查看英文原文,
原文链接