Swift   发布时间:2022-04-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了自定义文本颜色和UITabBarItem的字体导致怪异导致swift大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试更改我的UITabBarItems的文本,并使用了诸如 this之类的问题.第二个答案对我很有用,除非我尝试调整UITabBarItem的字体.此代码生成所选文本的预期结果为白色,未选中的项目为浅灰色:

UITabBarItem.appearance().settitleTextAttributes([NSForegroundColorAttributename: UIColor.blackColor()],forState:.Normal)

UITabBarItem.appearance().settitleTextAttributes([NSForegroundColorAttributename: UIColor.whiteColor()],forState:.SELEcted)

@L_301_1@

但是,如果添加

UITabBarItem.appearance().settitleTextAttributes([NSFontAttributename:UIFont(name: "American Typewriter",size: 13)!],forState: .SELEcted)

自定义文本颜色和UITabBarItem的字体导致怪异导致swift

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

奇怪的是,如果我更改.在最后一个片段中选择.Normal,那么所选文本将变为白色,文本将与代码中的字体匹配.

自定义文本颜色和UITabBarItem的字体导致怪异导致swift

UITabBarItem.appearance().settitleTextAttributes([NSFontAttributename:UIFont(name: "American Typewriter",forState: .Normal)

这几乎是完美的,但是未选择的文本现在没有变化.我不确定我做错了什么或这是一个错误,但如果有任何其他方法来完成这项任务,我很乐意听到它.

基于dfri的评论,我试过这个:

UITabBarItem.appearance().settitleTextAttributes([NSForegroundColorAttributename: UIColor.blackColor(),NSFontAttributename : [NSFontAttributename:UIFont(name: "American Typewriter",size: 13)!]],forState:.SELEcted)

UITabBarItem.appearance().settitleTextAttributes([NSForegroundColorAttributename : UIColor.whiteColor(),forState:.Normal)

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

这对我没有任何意义

解决方法

请尝试以下方法

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)

大佬总结

以上是大佬教程为你收集整理的自定义文本颜色和UITabBarItem的字体导致怪异导致swift全部内容,希望文章能够帮你解决自定义文本颜色和UITabBarItem的字体导致怪异导致swift所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。