【问题标题】:Change UITabBarItem badge font in iOS 12在 iOS 12 中更改 UITabBarItem 徽章字体
【发布时间】:2019-06-13 22:45:29
【问题描述】:

我想更改我的 tabbarItem 徽章字体,我尝试使用 this answer 执行此操作,但正如它在 cmets 中所说,它适用于 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 谢谢,有自定义徽章的库吗?

标签: ios swift uitabbaritem badge


【解决方案1】:

我搜索了很多,发现无法更改标签栏项目徽章字体。所以我想为 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)
        }
    }
  }
} 

【讨论】:

    猜你喜欢
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 2014-02-23
    • 2012-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多