我有一个 TabBar。我正在尝试设置它的样式,以便 TabBarItems 上的标题在正常状态和选定状态下具有不同的字体。对于正常状态,我想要 Helvetica Neue Light,对于选定状态,我想要 Helvetica Neue Medium。无论我做什么,我似乎都无法让这两个州的字体不同。颜色变化效果很好。这是我目前拥有的:
// 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];
请帮忙。
最佳答案
好消息和坏消息。
坏消息,这比仅使用外观代理要难一些。
好消息并没有那么难!
标题
#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 或您有什么。
但是是的!最重要的是,只需更新 ViewController tabBarItem!
关于ios - 更改选定的 TabBarItem 字体 iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25234671/