更改选定的TabBarItem字体iOS [英] Changing selected TabBarItem font iOS

查看:89
本文介绍了更改选定的TabBarItem字体iOS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个TabBar。我试图设置它的样式,以便TabBarItems上的标题具有正常状态和选定状态的不同字体。对于正常状态,我想要Helvetica Neue Light,对于选定状态,我想要Helvetica Neue Medium。无论我做什么,我似乎都无法让这两种状态的字体不同。颜色变化很好。以下是我目前的情况:

I have a TabBar. I am trying to style it so that the titles on the TabBarItems have different fonts for the normal state and the selected state. For normal state I want Helvetica Neue Light and for the selected state I want Helvetica Neue Medium. No matter what I do, I can't seem to get the fonts to be different for these two states. The color changing works fine. Here is what I currently have:

  // Set the tab bar title appearance for normal state.
  [[UITabBarItem appearance] setTitleTextAttributes:@{
     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light"
                                          size:16],
     NSForegroundColorAttributeName: [CMK8Colors grayColor]
   }
                                           forState:UIControlStateNormal];

  // Set the tab bar title appearance for selected state.
  [[UITabBarItem appearance] setTitleTextAttributes:@{
     NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Medium"
                                          size:16],
     NSForegroundColorAttributeName: [CMK8Colors blueColor]
   }
                                           forState:UIControlStateSelected];

请帮忙。

推荐答案

好消息和坏消息。

坏消息,这比仅使用外观代理要困难一些。

Bad news, It's a little harder than just using the appearance proxy.

好消息这不是那么难!

Header

#import <UIKit/UIKit.h>

@interface MYTabbyViewController : UITabBarController

@end

实现

#import "MYTabbyViewController.h"

@implementation MYTabbyViewController

-(void)setSelectedViewController:(UIViewController *)selectedViewController
{
    [super setSelectedViewController:selectedViewController];

    for (UIViewController *viewController in self.viewControllers) {
        if (viewController == selectedViewController) {
            [viewController.tabBarItem setTitleTextAttributes:@{
                                                    NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Medium" size:16],
                                                    NSForegroundColorAttributeName: [UIColor blueColor]
                                                    }
                                         forState:UIControlStateNormal];
        } else {
            [viewController.tabBarItem setTitleTextAttributes:@{
                                                    NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue-Light" size:16],
                                                    NSForegroundColorAttributeName: [UIColor grayColor]
                                                    }
                                         forState:UIControlStateNormal];
        }
    }
}

您需要的最后一部分是使用这个子类而不是开箱即用的UITabBarController。如果您正在使用Storyboard,只需选择TabBarController,转到Identity Inspector(右侧面板中的第三个子选项卡)并将UITabBarController更改为MYTabBarController或者您有什么。

The last part you will need is to use this subclass instead of the out-of-the-box UITabBarController. If you are using Storyboards, simply select the TabBarController, go to the Identity Inspector (third subtab in the right panel) and change UITabBarController to MYTabBarController or what have you.

但是是啊!底线,只需更新ViewController tabBarItem!

But yeah! Bottom line, just update the ViewController tabBarItem!

这篇关于更改选定的TabBarItem字体iOS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆