美文网首页
iOS 设置 tabbar 选中状态字体大小

iOS 设置 tabbar 选中状态字体大小

作者: 桀骜不驯的搬砖者 | 来源:发表于2019-03-01 15:46 被阅读0次
    首先先介绍我们通常怎么设置普通状态和选中状态的字体和颜色和图片
    #如果设置选中按钮的图片不生效,是因为呗系统默认渲染了,请设置图片的渲染模式:imageWithRenderingMode:为 UIImageRenderingModeAlwaysOriginal
    自定义 BRTabBarController: UITabBarController
       [[UITabBarItem appearance] setImage:[[UIImage imageNamed:@"1.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        [[UITabBarItem appearance] setSelectedImage:[[UIImage imageNamed:@"2.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
        NSDictionary *textTitleOptions = @{NSForegroundColorAttributeName:[UIColor whiteColor], ,NSFontAttributeName:[UIFont systemFontOfSize:10]};
        NSDictionary *textTitleOptionsSelected = @{NSForegroundColorAttributeName:[UIColor redColor], ,NSFontAttributeName:[UIFont systemFontOfSize:12]};
        [nav.tabBarItem setTitleTextAttributes:textTitleOptions forState:UIControlStateNormal];
        [nav.tabBarItem setTitleTextAttributes:textTitleOptionsSelected forState:UIControlStateSelected];
        [nav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(2, -2)]; //设置tabbar上的文字的位置
    
    坑:你会发现选中状态下的字体设置无效(颜色是有效的),但是如果你只设置普通状态下的文字大小,选中状态下的也会跟着改变,猜想是因为系统那普通状态小的字体大小做渲染吧。
    解决方案:原理:既然设置普通状态下有效,那么我们在选中的时候设置这个 item 的普通状态下的字体即可。
    //实现 UITabBarDelegate 代理
    - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
    {
    //重新设置所有 item 的字体
        for (UITabBarItem *unSelItem in tabBar.items) {
            if (unSelItem == item) {//选中的设置他的状态
                NSDictionary *textTitleOptions = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12]};
                [item setTitleTextAttributes:textTitleOptions forState:UIControlStateNormal];
            }else {//未选中的设置他的状态
                NSDictionary *textTitleOptions = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
                [unSelItem setTitleTextAttributes:textTitleOptions forState:UIControlStateNormal];
            }
        }
    }
    

    如果有用的,拿去用,没用的就另寻高明吧。

    相关文章

      网友评论

          本文标题:iOS 设置 tabbar 选中状态字体大小

          本文链接:https://www.haomeiwen.com/subject/jtmduqtx.html