自定义 UITabBarItem 的文本颜色和字体导致 swift 出现奇怪的结果

Posted

技术标签:

【中文标题】自定义 UITabBarItem 的文本颜色和字体导致 swift 出现奇怪的结果【英文标题】:Customizing text color AND font of UITabBarItem causing weird result in swift 【发布时间】:2015-12-30 22:21:15 【问题描述】:

我正在尝试更改我的UITabBarItems 的文本并使用了诸如this 之类的问题。除非我尝试调整UITabBarItem 的字体,否则第二个答案对我很有用。这个 sn-p 产生的预期结果是选中的文本是白色的,未选中的项目是浅灰色的:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState:.Normal)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Selected)

但是,如果添加了这个:

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!], forState: .Selected)

由于某种原因,文本在被选中和未选中时都会变为黑色,并且字体保持不变。

奇怪的是,如果我在最后一个 sn-p 中将 .Selected 更改为 .Normal,那么选定的文本会变成白色,并且文本会与代码中的字体相匹配。

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!], forState: .Normal)

这几乎是完美的,但是未选择的文本现在没有改变。我不确定是我做错了什么还是这是一个错误,但如果有其他方法可以完成这项任务,我很想听听。

基于 dfri 的 cmets,我已经尝试过:

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor(),
        NSFontAttributeName : [NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!]], forState:.Selected)

UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName : UIColor.whiteColor(),
        NSFontAttributeName : [NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!]], forState:.Normal)

现在应用程序崩溃了。错误说:

无法识别的选择器发送到实例 0x7fa6d9461ef0

这对我来说没有任何意义

【问题讨论】:

@dfri 这会导致与第二个 sn-p 相同的结果,其中文本始终为黑色 @dfri 我认为我正确地解释了您的评论,我应该尝试同时设置颜色和字体,但是当我尝试它使应用程序崩溃时。检查更新的问题。当属性存储在外部数组中时,也会出现同样的结果 @dfri 做到了!非常感谢 太好了,乐于助人! 我想让字体加粗。我怎么能那样做?有人吗? 【参考方案1】:

试试下面的

let colorNormal : UIColor = UIColor.blackColor()
let colorSelected : UIColor = UIColor.whiteColor()
let titleFontAll : UIFont = UIFont(name: "American Typewriter", size: 13.0)!

let attributesNormal = [
    NSForegroundColorAttributeName : colorNormal,
    NSFontAttributeName : titleFontAll
]

let attributesSelected = [
    NSForegroundColorAttributeName : colorSelected,
    NSFontAttributeName : titleFontAll
]

UITabBarItem.appearance().setTitleTextAttributes(attributesNormal, forState: .Normal)
UITabBarItem.appearance().setTitleTextAttributes(attributesSelected, forState: .Selected)

【讨论】:

这不会为我更改 .selected 状态的字体。 @AbbasAngouti 确保将其添加到 AppDelegate 类中。应该可以正常工作。【参考方案2】:

对于ios 10及以上,您只需更改.normal的字体,这将影响选中和未选中项的字体。

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName : UIFont.myMediumFont(withSize: 10)], for: [.normal])

此外,您可以像这样设置 tintColors 而不使用 .setTitleTextAttributes:

UITabBar.appearance().unselectedItemTintColor = UIColor.white
UITabBar.appearance().tintColor = UIColor.black

【讨论】:

以上是关于自定义 UITabBarItem 的文本颜色和字体导致 swift 出现奇怪的结果的主要内容,如果未能解决你的问题,请参考以下文章

如何在 iOS 4 中更改 UITabBarItem 中文本的颜色

设置自定义字体以及文本颜色

在 UINavigationController 中自定义 UITabBarItem

如何为 UITabBarItem 设置自定义标题属性

是否可以自定义 UITabBarItem 徽章?

如何在 UITabBarItem 上设置不同的未选择图像和文本颜色