我想改变我的文字UITabBarItems
,并使用问题,比如这个.第二个答案对我很有用,除非我尝试调整字体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)
由于某种原因,当文本被选中和未被选中时,文本变为黑色,并且字体保持不变.
奇怪的是,如果我更改.Selected
到.Normal
最后一个片段,那么所选文本将变为白色,文本将与代码中的字体匹配.
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!], 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(), NSFontAttributeName : [NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!]], forState:.Normal)
现在应用程序崩溃了.错误说:
无法识别的选择器发送到实例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)