更改选定的TabBarItem字体iOS

问题描述 投票:8回答:4

我有一个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];

请帮忙。

ios uitabbar uitabbaritem
4个回答
11
投票

好消息和坏消息。

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

好消息它并没有那么难!

#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!


6
投票

@ Acey在Swift 3中回答:

    override var selectedViewController: UIViewController? {
    didSet {

        guard let viewControllers = viewControllers else {
            return
        }

        for viewController in viewControllers {

            if viewController == selectedViewController {

                let selected: [String: AnyObject] =
                    [NSFontAttributeName: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy),
                     NSForegroundColorAttributeName: UIColor.red]

                viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)

            } else {

                let normal: [String: AnyObject] =
                    [NSFontAttributeName: UIFont.systemFont(ofSize: 11),
                     NSForegroundColorAttributeName: UIColor.blue]

                viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)

            }
        }
    }
}

2
投票

@Acey's Answer几乎是完美的,但只在第一次选择tabBarItem后才更改字体,所以当创建tabBar时,它将在那一刻创建时所选tabBarItem没有不同的字体。为了达到这个目的,我还在didSet变量中添加了selectedIndex

这是完整的代码(Swift 4.2)

final class TabBarController: UITabBarController {

override var selectedIndex: Int {
    didSet {
        guard let selectedViewController = viewControllers?[selectedIndex] else {
            return
        }
        selectedViewController.tabBarItem.setTitleTextAttributes([.font: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy)], for: .normal)
    }
}

override var selectedViewController: UIViewController? {
    didSet {

        guard let viewControllers = viewControllers else {
            return
        }

        for viewController in viewControllers {

            if viewController == selectedViewController {

                let selected: [NSAttributedString.Key: AnyObject] =
                    [.font: UIFont.systemFont(ofSize: 11, weight: UIFontWeightHeavy)]

                viewController.tabBarItem.setTitleTextAttributes(selected, for: .normal)

            } else {

                let normal: [NSAttributedString.Key: AnyObject] =
                    [.font: UIFont.systemFont(ofSize: 11)]

                viewController.tabBarItem.setTitleTextAttributes(normal, for: .normal)

            }
        }
    }
}

}


1
投票

setSelected和setHighlighted对UIBarButtonItems不起作用。

使用UIBarButtonItems

- (void)setBackgroundImage:(UIImage *)backgroundImage forState:(UIControlState)state barMetrics:(UIBarMetrics)barMetrics 

这是文档 - https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIBarButtonItem_Class/Reference/Reference.html

或者,使用UIButton。

UIButton *myButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[myButton setTitle:@"Info" forState:UIControlStateNormal];
[myButton setFont:[UIFont fontWithName:<font name> size:<font size>]];

[myButton setTitleColor:<color>;
myButton =  <frame>;
[myButton addTarget:self.webView action:@selector(<action>:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc]initWithCustomView:myButton];
[[UIBarButtonItem appearance] setTintColor:<color>;
[self.toolbar setItems:[NSArray arrayWithObjects: spaceItem, barButtonItem, nil]];
© www.soinside.com 2019 - 2024. All rights reserved.