更改tabbaritem的字体大小

时间:2010-04-05 01:58:56

标签: iphone ios cocoa-touch uitabbar uitabbaritem

是否可以更改标签的字体大小?

10 个答案:

答案 0 :(得分:60)

我建议采用更好的方法:

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

答案 1 :(得分:20)

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

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

答案 2 :(得分:14)

  

IN 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)

}
  

在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)
}

答案 3 :(得分: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: ...];
}

答案 4 :(得分:7)

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

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

答案 5 :(得分: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];
        }
    }
}

答案 6 :(得分: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 :(得分: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]];
            }
        }
    }
}

答案 8 :(得分:0)

  

在Swift 4中

 override func viewDidLoad() {
        super.viewDidLoad()
        let appearance = UITabBarItem.appearance()
        let attributes: [NSAttributedString.Key: AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue):UIFont(name: "American Typewriter", size: 12)!, NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.orange]
        appearance.setTitleTextAttributes(attributes, for: .normal)

    }

答案 9 :(得分:0)

我认为这在Swift中可以对选项卡栏的颜色和文本属性进行清晰的控制。

class MyTabBarController: UITabBarController {

  override func viewDidLoad() {
    super.viewDidLoad()
    tabBar.barTintColor = UIColor.green

    UITabBarItem.appearance().setTitleTextAttributes(
        [NSAttributedString.Key.font:UIFont.boldSystemFont(ofSize: 18), 
           NSAttributedString.Key.foregroundColor: UIColor.orange], for: .normal)
...