加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:52:52 所属栏目:百科 来源:网络整理
导读:我正在尝试更改我的UITabBarItems的文本,并使用了诸如 this之类的问题.第二个答案对我很有用,除非我尝试调整UITabBarItem的字体.此代码段生成所选文本的预期结果为白色,未选中的项目为浅灰色: UITabBarItem.appearance().setTitleTextAttributes([NSForegro
我正在尝试更改我的UITabBarItems的文本,并使用了诸如 this之类的问题.第二个答案对我很有用,除非我尝试调整UITabBarItem的字体.此代码段生成所选文本的预期结果为白色,未选中的项目为浅灰色:

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

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

enter image description here

但是,如果添加:

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

enter image description here

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

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

enter image description here

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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读