2010-04-05 111 views

回答

0

其实有一种方法。

NSMutableArray *tabBarItems = [[[[[self.view subviews] objectAtIndex:1] subviews] mutableCopy] autorelease]; 

for (int item = 0; item < [tabBarItems count]; item++) { 
    for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) { 
     for (int item = 0; item < [tabBarItems count]; item++) { 
      for (int subview = 0; subview < [[[tabBarItems objectAtIndex:item] subviews] count]; subview++) { 
       if ([[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")]) 
        [[[[tabBarItems objectAtIndex:item] subviews] objectAtIndex:subview] setFont:[UIFont systemFontOfSize:6.0f]]; 
      } 
     } 
    } 
} 
5
TabBarIncreaseFonts(self.tabBarController); 


void TabBarIncreaseFonts(UITabBarController* customTabBarController) 
{ 

    for(UIView* controlLevelFirst in [customTabBarController.tabBar subviews]) 
    { 

     if(![controlLevelFirst isKindOfClass:NSClassFromString(@"UITabBarButton")]) 
      continue; 

     for(id controlLevelSecond in [controlLevelFirst subviews]) 
     { 
      [controlLevelSecond setBounds: CGRectMake(0, 0, 100, 48)]; 

      if(![controlLevelSecond isKindOfClass:NSClassFromString(@"UITabBarButtonLabel")]) 
       continue; 

      [controlLevelSecond setFont: [UIFont boldSystemFontOfSize:20]]; 
      [controlLevelSecond setFrame: CGRectMake(0, 0, 96, 48)]; 
      [controlLevelSecond setTextAlignment:UITextAlignmentCenter]; 
     } 
    } 
} 
59

我推荐一个更好的办法:

[yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIColor whiteColor], UITextAttributeTextColor, 
    [NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset, 
    [UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil] 
    forState:UIControlStateNormal]; 
+9

它只能在iOS5.0(或更高版本).. – Kjuly 2011-12-28 05:37:37

+0

不错,它的作品精美绝伦,我..谢谢 – Gaurav 2012-07-22 14:22:47

+0

哪个地方你需要写上面这段代码?我尝试在[self.tabBarController setViewControllers:aControllerList animated:YES]之后使用它;但这并没有帮助。 – Abhinav 2012-09-17 21:46:16

20
for(UIViewController *tab in self.tabBarController.viewControllers) 

{   
    [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
    [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil] 
    forState:UIControlStateNormal]; 
} 
+0

更新:请参阅voghDev对iOS 7.0+版本的回答,它避免了弃用的UITextAttributeFont。 – ToolmakerSteve 2017-02-16 19:07:29

1

[离开这个在这里为自己的参考,只是一个即兴过其他工作的答案。对于MEM它适用于iOS 7的修复,这超出的提问有点...]

@implementation UITabBarController (Util) 

- (void) fixForIos7 { 
    if (!IS_IOS7) 
     return; 
    UIFont *tabBarFont = [UIFont systemFontOfSize:12]; 
    NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys: 
      tabBarFont, UITextAttributeFont, nil]; 
    for(UIViewController *tab in self.viewControllers) { 
     [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal]; 
    } 
} 
@end 

缺少方法是

#define IS_IOS7 (UIDevice.currentDevice.systemVersion.floatValue > 6.9) 
7

简单的iOS中5.0或更高版本:

[[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal]; 
+0

好的解决方案,但使用'NSFontAttributeName'而不是'UITextAttributeFont' – Laszlo 2016-08-30 09:43:05

9

[更新] iOS 7.0以上版本的版本@ cancer86的很好的回答:

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor whiteColor], NSForegroundColorAttributeName, 
                [UIFont fontWithName:@"Helvetica" size:tabFontSize], 
                NSFontAttributeName, 
                nil] forState:UIControlStateNormal]; 

[[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys: 
                [UIColor redColor], NSForegroundColorAttributeName, 
                [UIFont fontWithName:@"Helvetica" size:tabFontSize], NSFontAttributeName, 
                nil] forState:UIControlStateSelected]; 

主要的变化是,UITextAttributeTextColor和UITextAttributeFont都不赞成

为了将其应用到所有标签(感谢@ToolmakerSteve您指出)

for(UIViewController *tab in self.tabBarController.viewControllers) 
{   
    [tab.tabBarItem setTitleTextAttributes: ...]; 
} 
+0

..应用于所有标签,从spatil的答案'for(UIViewController * tab in self.tabBarController.viewControllers)[tab.tabBarItem setTitleTextAttributes:...' – ToolmakerSteve 2016-12-21 21:26:44

+0

好的反应更新 – voghDev 2016-12-22 08:45:25

+0

我们可以有快速的3版本 – 2017-02-03 12:04:42

12

斯威夫特2.0

override func viewDidLoad() { 
    super.viewDidLoad() 

    let appearance = UITabBarItem.appearance() 
    let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orangeColor()] 
    appearance.setTitleTextAttributes(attributes, forState: .Normal) 

} 

In Swift 3.0

override func viewDidLoad() { 
    super.viewDidLoad() 

    let appearance = UITabBarItem.appearance() 
    let attributes: [String: AnyObject] = [NSFontAttributeName:UIFont(name: "American Typewriter", size: 12)!, NSForegroundColorAttributeName: UIColor.orange] 
    appearance.setTitleTextAttributes(attributes, for: .normal) 
}