ios - 在 iOS 12 中更改 UITabBarItem 徽章字体
问题描述
我想更改我的 tabbarItem 徽章字体,我尝试使用这个答案来做到这一点,但正如评论中所说,它适用于 iOS 10,但在 iOS 11 中存在问题。我试图在 iOS 12 中使用 swift 4 但它不起作用。
UITabBarItem.appearance().setBadgeTextAttributes([NSAttributedStringKey.font.rawValue: UIFont(name: "IRANSans-Bold", size: 14) as Any], for: .normal)
请让我知道是否有任何方法可以执行此操作,或者我需要为此创建一个自定义类。
解决方案
我搜索了很多,发现无法更改标签栏项目徽章字体。因此,我为 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)
}
}
}
}
推荐阅读
- ios - 视频在 ios 的 inappbrowser 中每秒都会暂停,而如果音频关闭,则它可以完美播放
- javascript - 我想解析 node.js 中 request.on('row) 中的所有行 - 但它只能用一行
- php - Laravel 8:在字符串上调用成员函数 notify()
- javascript - 现在使用 dotenv 并没有给出任何东西,在它至少给出之前
- python - 希望机器人只从用户那里获得输入 3 次
- react-native - 由于 glog 库构建错误,在构建新的 react native iOS 应用程序时出错
- python - Python通过条件格式化字典
- sql - SQL查询中\r\n有什么用
- c# - Stripe Checkout 会话的有效期是多久?
- javascript - 如何使用JS在特定列之前添加列