我有一台UITabBar。我设置了选项卡栏项目的值:
tabBarItem3?.badgeValue = String(self.numberOfProducts)现在,我想将其字体更改为特定的字体。然后我使用了下面的代码:
tabBarItem3?.setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .normal)它不起作用。我该怎么办?
发布于 2019-04-24 01:13:53
尝试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)) Swift 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):让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):让bottomBar = UITabBarController()
..。
..。
..。
字体(bottomBar.setBadgeFont: 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)
}
}
}
}https://stackoverflow.com/questions/44478417
复制相似问题