ios 更改 tabbaritem 的字体大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2576592/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 17:06:21  来源:igfitidea点击:

Changing font size of tabbaritem

iphoneioscocoa-touchuitabbaruitabbaritem

提问by 4thSpace

Is it possible to change the font size of tabs?

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

回答by Cancer86

I recommend a better way:

我推荐一个更好的方法:

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

回答by spatil

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

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

回答by Ashok R

IN Swift 2.0

在斯威夫特 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

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

回答by voghDev

[Update] iOS 7.0+ version of @cancer86's nice answer:

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

The main change is that UITextAttributeTextColor and UITextAttributeFont are both deprecated

主要的变化是 UITextAttributeTextColor 和 UITextAttributeFont 都被弃用了

In order to apply it to all tabs (thanks to @ToolmakerSteve for pointing out)

为了将其应用于所有选项卡(感谢@ToolmakerSteve 指出)

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

回答by Fei Yang

Simple in iOS 5.0 or later:

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

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

回答by horea

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

回答by Sreeraj VR

IN Swift 4

在斯威夫特 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)

    }

回答by Dan Rosenstark

[leaving this here for my own reference, just a riff off the other working answers. For mem it's a fix for iOS 7, which is beyond the question by a bit...]

[把这个留在这里供我自己参考,只是对其他工作答案的即兴演奏。对我来说,这是对 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

the missing method is

缺少的方法是

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

回答by Gi-lo

Actually there is a way.

其实是有办法的。

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

回答by Jan Bergstr?m

I think this in Swift gives a clear control over the tab bar colors and text attributes.

我认为这在 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)
...