3

I want change my tabbarItem badge font and I tried to do this using this answer but as it says in the comments it works for iOS 10 but there are issues in iOS 11. I'm trying to do this in iOS 12 using swift 4 but it doesn't work.

UITabBarItem.appearance().setBadgeTextAttributes([NSAttributedStringKey.font.rawValue: UIFont(name: "IRANSans-Bold", size: 14) as Any], for: .normal)

Please let me know if there is anyway to do this or I need to make a custom class for that.

Let's_Create
  • 2,963
  • 3
  • 14
  • 33
Niloufar
  • 512
  • 1
  • 5
  • 24
  • Played with it for a small time, and it seems like it doesn't allow font modification on the tabbar item badge this way. Changing the tabbar font works, but not the badge font somehow... – balazs630 Jan 20 '19 at 11:38
  • @balazs630 thanks, so any library for custom badge? – Niloufar Jan 20 '19 at 11:47

1 Answers1

0

I searched a lot and found out that it is not possible to change tab bar item badge font. So I came out with writing an extension for UITabBarController as follow:

    func setCustomBadge(){
    removeMyCustomBadge(view: self.tabBar)
    setBadgeLabel(view: self.tabBar)
}
func setBadgeLabel(view:UIView){
    for subview in (view.subviews){
        let myType = String(describing: type(of: subview))
        print(myType)
        if myType == "_UIBadgeView" {
          let customBadeg : UILabel = UILabel(frame: CGRect(x: subview.frame.origin.x, y: subview.frame.origin.y, width: subview.frame.size.width, height: subview.frame.size.height))
            customBadeg.font = UIFont(name: "IRANSans", size: 10)
            customBadeg.layer.masksToBounds = true
            customBadeg.layer.cornerRadius = customBadeg.frame.size.height/2
            customBadeg.textAlignment = .center
            customBadeg.textColor = UIColor.white
            customBadeg.backgroundColor = UIColor(named: "Red")
            customBadeg.text = Utility().enNums2Fa(inputString: String(format: "%d", Utility().retrieveUserBasketBadge()))
            view.addSubview(customBadeg)
            subview.isHidden = true
        }
        else {
            setBadgeLabel(view:subview)
        }
    }
  }
} 
Niloufar
  • 512
  • 1
  • 5
  • 24