where / when /如何设置tabBarItem字体属性属性?

时间:2014-11-24 00:16:12

标签: ios objective-c uitabbaritem

我正在尝试为我的标签栏项目增大字体。我试过在AppDelegate中这样做:

MenuTableViewController *mvc = [[MenuTableViewController alloc] init];
mvc.delegate = self;
mvc.restorationIdentifier = NSStringFromClass([mvc class]);
mvc.restorationClass = [self class];
UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:mvc];
SuperTabBarController *nav = [[SuperTabBarController alloc] init];
JudgeViewController *jvc = [[JudgeViewController alloc] init];
BuzzViewController *bvc = [[BuzzViewController alloc] init];
nav.viewControllers = @[jvc, bvc, nav1];

UIFont *tabBarFont = [UIFont systemFontOfSize:40];
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     tabBarFont, NSFontAttributeName, nil];
for(UIViewController *tab in  nav.viewControllers) {
    [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
    NSLog(@"yo normal");
}

for(UIViewController *tab in  nav.viewControllers) {
    [tab.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateSelected];
    NSLog(@"yo");      
}


[UIView transitionWithView:self.window duration:0.5 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{self.window.rootViewController = nav;} completion:nil];

我也尝试过像这样定义字典:

   NSDictionary *titleTextAttributes2 = @{NSFontAttributeName:
                                               [UIFont fontWithName:@"Helvetica" size:20]};

这是一个自定义函数,一旦用户登录就通过委托协议调用。我的NSLogs打印出来,但我也在控制台中得到它:

 button text attributes only respected for UIControlStateNormal, UIControlStateSelected and UIControlStateDisabled. state = 1 is interpreted as UIControlStateSelected.

除了日志输出之外,程序还会根据需要运行,除了标题字体根本不会改变。

然后我把它放在我的自定义视图控制器的viewDidLoad中:

UIFont *tabBarFont = [UIFont systemFontOfSize:40];
NSDictionary *titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                     tabBarFont, NSFontAttributeName, nil];

    [self.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateNormal];
    [self.tabBarItem setTitleTextAttributes:titleTextAttributes forState:UIControlStateSelected];

但这也不会影响标签栏标题的字体大小。

我应该在哪里执行这些命令?

谢谢。


编 标记的答案可以满足我的需要,但我注意到只有使用类范围的setter而不是实例setter才能使用该方法。因此,要全局更改标签栏项目标题文本,这就像魅力:

NSDictionary *attribute =     @{NSFontAttributeName:
                                    [UIFont fontWithName:@"Helvetica" size:40]};
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];

但是尝试设置单个类实例的属性仍然无效(这些都在同一个函数中调用,但只有一个生效)。

NSDictionary * attribute2 = @ {NSFontAttributeName:                                      [UIFont fontWithName:@“Helvetica”size:10]}; [nav1.tabBarItem setTitleTextAttributes:attribute2 forState:UIControlStateNormal];

任何人都知道这是为什么?我已多次浏览Apple Dev docs而没有看到为什么类实例调用没有做任何事情。

1 个答案:

答案 0 :(得分:0)

我在didFinishLaunchingWithOptions中使用了此代码,它对我有用。

NSDictionary *attribute = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
[[UITabBarItem appearance] setTitleTextAttributes:attribute forState:UIControlStateNormal];