在iOS 12中更改UITabBarItem徽章字体

3

我想要更改我的标签栏项目徽章字体,我尝试使用这个答案来实现,但是评论中说它适用于iOS 10,而在iOS 11中存在问题。我正在尝试在iOS 12中使用Swift 4来实现,但它不起作用。

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

如果有任何方法可以完成这个任务,或者我需要为此创建自定义类,请告诉我。


玩了一会儿,似乎这种方式不允许修改选项卡栏项目徽章的字体。更改选项卡栏字体可以实现,但是徽章字体却不行... - balazs630
@balazs630 谢谢,那么有没有自定义徽章的库? - Niloufar
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)
        }
    }
  }
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接