I am trying to change the text of my UITabBarItems
and have used questions such as this. The second answer works great for me unless I try to adjust the font of the UITabBarItem
. This snippet produces the expected results of the selected text being white and the unselected item being light gray:
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.blackColor()], forState:.Normal)
UITabBarItem.appearance().setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.whiteColor()], forState:.Selected)
However, if this is added :
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!], forState: .Selected)
For some reason the text becomes black when it is both selected and unselected and the font remains unchanged.
Weirdly enough if I change .Selected
to .Normal
in the last snippet, then the selected text turns white and the text is made to match the font in the code.
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName:UIFont(name: "American Typewriter", size: 13)!], forState: .Normal)
This is almost perfect however the unselected text is now unchanged. Im not sure if I am doing something wrong or this is a bug, but if there are any other methods to completing this task, I would love to hear it.
Based on dfri's comments I have tried this:
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)
and now the app is crashing. The error says :
unrecognized selector sent to instance 0x7fa6d9461ef0
which does not make any sense to me
Try the following
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)
For iOS 10 and above, you only need to change the font for .normal, this will affect fonts on both selected and unselected items.
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName : UIFont.myMediumFont(withSize: 10)], for: [.normal])
Also, you can set the tintColors without using .setTitleTextAttributes like so:
UITabBar.appearance().unselectedItemTintColor = UIColor.white
UITabBar.appearance().tintColor = UIColor.black
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With