2010-04-05 146 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) 
}