在iOS 12中更改UITabBarItem徽章字体

问题描述 投票:2回答:1

我想要更改我的tabbarItem徽章字体,我尝试使用this answer这样做,但正如它在评论中说的那样适用于iOS 10但iOS 11中存在问题。我试图在iOS 12中使用swift 4进行此操作但它不起作用。

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

无论如何都要告诉我,或者我需要为此制作一个自定义课程。

ios swift uitabbaritem badge
1个回答
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)
        }
    }
  }
} 
© www.soinside.com 2019 - 2024. All rights reserved.