代码之家  ›  专栏  ›  技术社区  ›  4thSpace wkw

更改tabbaritem的字体大小

  •  33
  • 4thSpace wkw  · 技术社区  · 14 年前

    是否可以更改选项卡的字体大小?

    8 回复  |  直到 8 年前
        1
  •  60
  •   Sergey Glotov Nitesh Khosla    13 年前

    我推荐一个更好的方法:

    [yourTabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
        [UIColor whiteColor], UITextAttributeTextColor, 
        [NSValue valueWithUIOffset:UIOffsetMake(0,0)], UITextAttributeTextShadowOffset, 
        [UIFont fontWithName:@"Helvetica" size:18.0], UITextAttributeFont, nil]
        forState:UIControlStateNormal];
    
        2
  •  20
  •   spatil    12 年前
    for(UIViewController *tab in  self.tabBarController.viewControllers)
    
    {        
      [tab.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
      [UIFont fontWithName:@"Helvetica" size:20.0], UITextAttributeFont, nil]
      forState:UIControlStateNormal];
    }
    
        3
  •  14
  •   Ashok R    8 年前

    在斯威夫特2

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

    在斯威夫特3

    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)
    }
    
        4
  •  9
  •   voghDev    8 年前

    [更新]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];
    

    主要的变化是uitexttributetextcolor和uitexttributefont都被弃用。

    以便将其应用于所有选项卡(感谢@toolmakersteve指出)

    for(UIViewController *tab in  self.tabBarController.viewControllers)
    {        
        [tab.tabBarItem setTitleTextAttributes: ...];
    }
    
        5
  •  7
  •   Fei Yang    10 年前

    在iOS 5.0或更高版本中很简单:

    [[UITabBarItem appearance] setTitleTextAttributes:@{UITextAttributeFont:[UIFont boldSystemFontOfSize:15]} forState:UIControlStateNormal];
    
        6
  •  5
  •   Chris Frederick    13 年前
    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];
            }
        }
    }
    
        7
  •  1
  •   Dan Rosenstark    11 年前

    [把这个留给我自己参考,只是把其他有用的答案抄了一遍。对于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 )
    
        8
  •  0
  •   Gi-lo    14 年前

    其实是有办法的。

        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]];
                }
            }
        }
    }