I have been attempting to change the UITabBarItem text font, size and weight when it's the currently selected tab. I have achieved changing the color via UIControlState.Selected but for some reason the font is not changing. I have searched for 12 hours all over the interwebs and this seems to be an unsolved mystery! I am to the point where I believe there is a bug in Swift or XCode as the current functionality is strange!
My current code:
class CustomUITabBarItem: UITabBarItem {
// Setting Font Constants Here
let fontNormal = UIFont.systemFontOfSize(12)
let fontSelected = UIFont.systemFontOfSize(14, weight: UIFontWeightBold)
override func awakeFromNib() {
// Following line sets Normal state attributes and it works fine
self.setTitleTextAttributes([NSFontAttributeName: fontNormal, NSForegroundColorAttributeName: UIColor.grayColor()], forState: UIControlState.Normal)
// Following line should set Selected state attributes but it has unexpected behaviour
self.setTitleTextAttributes([NSFontAttributeName: fontSelected, NSForegroundColorAttributeName: UIColor.orangeColor()], forState: UIControlState.Selected)
}
}
The end result is perplexing because the UIControlState.Selected text only has the color attribute applied but the font type, size and weight do not change. I have fiddled around with the code for hours testing different functionality and even tried to figure out a round-about solution but I am at my wit's end.
Surely the .Selected property is active on the text because the color changes but it does not allow for font manipulation (but allows it in the .Normal state...), only color? What have I missed, or is there any way around this?
Extra information: Building to an iOS9.3.2 Device, Swift 2.2, XCode 7.3.1, OSX El Capitan 10.11.5