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

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

概述我正在尝试更改我的UITabBarItems的文本,并使用了诸如 this之类的问题.第二个答案对我很有用,除非我尝试调整UITabBarItem的字体.此代码段生成所选文本的预期结果为白色,未选中的项目为浅灰色: UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.bl 我正在尝试更改我的UITabbarItems的文本,并使用了诸如 this之类的问题.第二个答案对我很有用,除非我尝试调整UITabbarItem的字体.此代码段生成所选文本的预期结果为白色,未选中的项目为浅灰色:

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)

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

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

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)

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

unrecognized selector sent to instance 0x7fa6d9461ef0

这对我没有任何意义

解决方法 请尝试以下方法

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所遇到的程序开发问题。

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

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1013986.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存