在iOS 12中更改UITabBarItem标志字体

时间:2019-01-20 08:36:03

标签: ios swift uitabbaritem badge

我想更改我的tabbarItem徽章字体,我尝试使用this answer来执行此操作,但是正如注释中所述,它适用于iOS 10,但iOS 11中存在问题。使用Swift 4的iOS 12,但无法正常工作。

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

请告诉我是否有这样做的方法,或者我需要为此创建一个自定义类。

1 个答案:

答案 0 :(得分:0)

我进行了很多搜索,发现无法更改标签栏项目标志字体。因此,我想到了为UITabBarController编写扩展,如下所示:

    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)
        }
    }
  }
} 
相关问题