2010-04-05 61 views

Trả lời

0

Thực tế có một cách.

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

Tôi khuyên bạn nên một cách tốt hơn:

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

Nó chỉ hoạt động trong iOS5.0 (hoặc mới hơn) .. – Kjuly

+0

tốt đẹp, công trình của nó hoàn toàn tốt cho tôi .. cảm ơn – Gaurav

+0

Những nơi nào bạn cần phải viết trên đoạn mã? Tôi cố gắng sử dụng nó sau khi [self.tabBarController setViewControllers: aControllerList hoạt hình: YES]; nhưng điều này không giúp được gì. – Abhinav

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

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

CẬP NHẬT: Xem câu trả lời của voghDev cho phiên bản iOS 7.0+, tránh sử dụng UITextAttributeFont không dùng nữa. – ToolmakerSteve

1

[rời khỏi đây vào đây để tham khảo của riêng tôi, chỉ cần một cú riff tắt câu trả lời làm việc khác. Đối với các mem đó là một bản sửa lỗi cho iOS 7, đó là ngoài vấn đề đó bằng một chút ...]

@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 

phương pháp còn thiếu là

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

đơn giản trong iOS 5.0 trở lên:

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

giải pháp tốt, nhưng sử dụng 'NSFontAttributeName' thay vì' UITextAttributeFont' – Laszlo

9

[Update] iOS 7.0 trở lên phiên bản của câu trả lời thoải mái @ cancer86 của:

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

Sự thay đổi chính là UITextAttributeTextColor và UITextAttributeFont đều bị phản đối

Để áp dụng nó cho tất cả các tab (nhờ @ToolmakerSteve để chỉ ra)

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

.. để áp dụng cho tất cả các tab, từ câu trả lời của spatil 'cho (tab UIViewController * trong self.tabBarController.viewControllers) [tab.tabBarItem setTitleTextAttributes: ...' – ToolmakerSteve

+0

phản hồi ok được cập nhật – voghDev

+0

chúng ta có thể có 3 phiên bản nhanh cho việc này không –

12

TRÊN Swift 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) 

} 

Trong 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) 
} 
Các vấn đề liên quan