-1

I have a configuration code for UITabBarItem but it is too much. Is there any way to shorten this code? I couldn't find any extension for that.

let selectedColor = #colorLiteral(red: 0.3921568627, green: 0.168627451, blue: 0.4509803922, alpha: 1)
let deselectedColor = #colorLiteral(red: 0.5843137255, green: 0.6470588235, blue: 0.6509803922, alpha: 1)
    
let selectedFont = UIFont(name: "SFProDisplay-Bold", size: 10)!
let deselectedFont = UIFont(name: "SFProDisplay-Regular", size: 10)!
    
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: deselectedFont, NSAttributedString.Key.foregroundColor : deselectedColor], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSAttributedString.Key.font: selectedFont, NSAttributedString.Key.foregroundColor : selectedColor], for: .selected)

If there are any extensions for UITabBar and UITabBarItems, can you please share them with me?

grative
  • 21
  • 1
  • 4

1 Answers1

1

One thing you can do is to get rid of all NSAttributedString.Keys, because swift can infer that itself.

UITabBarItem.appearance().setTitleTextAttributes([.font: deselectedFont, .foregroundColor : deselectedColor], for: .normal)