我有UITabBar
。我为标签栏项设置了一个值:
tabBarItem3?.badgeValue = String(self.numberOfProducts)
现在我想将其字体更改为特定字体。然后我使用了这段代码:
tabBarItem3?.setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .normal)
它不起作用。我该怎么办?
答案 0 :(得分:3)
Swift 3
UITabBarItem.appearance().setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .normal)
UITabBarItem.appearance().setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .selected)
答案 1 :(得分:1)
UIKit会在视图layoutSubviews
或viewWillAppear
后的某个时间更新徽章字体。完全重写这将需要一些代码。您希望首先观察徽章的字体更改。
tabBarItem.addObserver(self, forKeyPath: "view.badge.label.font", options: .new, context: nil)
现在一旦调用了observe方法,就可以安全地设置徽章字体。但是有一个问题。 UIKit不会两次应用相同的更改。要解决此问题,首先将徽章属性设置为nil,然后重新应用您的字体。
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if keyPath == "view.badge.label.font" {
let badgeAttributes = [NSFontAttributeName: UIFont(name: "IRANSans", size: 14)]
tabBarItem?.setBadgeTextAttributes(nil, for: .normal)
tabBarItem?.setBadgeTextAttributes(badgeAttributes, for: .normal)
} else {
super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context)
}
}
只是未来的iOS更新,您可能希望将addObserver
包装在try-catch中。完成后也不要忘记删除观察者!
答案 2 :(得分:0)
尝试使用UITabBarController extension
中的这两个功能:
var tabBarController : UITabBarController
bottomBar = UITabBarController()
...
...
...
// Change badge font size
bottomBar.setBadgeFontSize(fontSize: 10.0)
// Change badge font
bottomBar.setBadgeFont(font: UIFont.boldSystemFont(ofSize: 12.0))
快捷键4
extension UITabBarController {
/**
Change the badge font size of an UITabBarController item.
- Parameter fontSize: new font size
- Parameter subviews: nil or optional when called from UITabBarController object.
Example of usage (programmatically):
```
let bottomBar = UITabBarController()
...
...
...
bottomBar.setBadgeFontSize(fontSize: 10.0)
```
*/
func setBadgeFontSize(fontSize: CGFloat, subviews: [UIView]? = nil) {
let arraySubviews = (subviews == nil) ? self.view.subviews : subviews!
for subview in arraySubviews {
let describingType = String(describing: type(of: subview))
if describingType == "_UIBadgeView" {
for badgeSubviews in subview.subviews {
let badgeSubviewType = String(describing: type(of: badgeSubviews))
if badgeSubviewType == "UILabel" {
let badgeLabel = badgeSubviews as! UILabel
badgeLabel.fontSize = fontSize
break
}
}
} else {
setBadgeFontSize(fontSize: fontSize, subviews: subview.subviews)
}
}
}
/**
Change the badge font size of an UITabBarController item.
- Parameter font: new font
- Parameter subviews: nil or optional when called from UITabBarController object.
Example of usage (programmatically):
```
let bottomBar = UITabBarController()
...
...
...
bottomBar.setBadgeFont(font: UIFont.boldSystemFont(ofSize: 12.0))
```
*/
func setBadgeFont(font: UIFont, subviews: [UIView]? = nil) {
let arraySubviews = (subviews == nil) ? self.view.subviews : subviews!
for subview in arraySubviews {
let describingType = String(describing: type(of: subview))
if describingType == "_UIBadgeView" {
for badgeSubviews in subview.subviews {
let badgeSubviewType = String(describing: type(of: badgeSubviews))
if badgeSubviewType == "UILabel" {
let badgeLabel = badgeSubviews as! UILabel
badgeLabel.font = font
break
}
}
} else {
setBadgeFont(font: font, subviews: subview.subviews)
}
}
}
}
答案 3 :(得分:-3)
Swift 3
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 10)], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 10)], for: .selected)