我想使用tabBarViewController中的setBadgeTextAttributes更改标签栏项目的徽章文字颜色和字体。所以我使用我的代码:
for (UITabBarItem *tabBarItem in self.tabBar.items){
[tabBarItem setBadgeColor:[UIColor redColor];
[tabBarItem setBadgeTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
[UIFont fontWithName:@"Helvetica-Bold" size:26.0], NSFontAttributeName,
[UIColor greenColor], NSForegroundColorAttributeName,
nil] forState:UIControlStateNormal];
}
但它只会更改徽章文字颜色,而不会更改字体。我该如何解决?
请,谢谢〜
答案 0 :(得分:1)
let attribute : [String : Any] = [NSAttributedStringKey.foregroundColor.rawValue : UIColor.black]
self.tabBarController?.tabBar.items?[tabBarIndex].setBadgeTextAttributes(attribute, for: .normal)
这就是我的做法
使用rawValue
答案 1 :(得分:0)
尝试将您的代码放在viewDidAppear中,如下所示:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let notificationItem = self.tabBar.items?.last {
notificationItem.badgeValue = "\(12)"
if #available(iOS 10.0, *) {
notificationItem.badgeColor = .red
notificationItem.setBadgeTextAttributes([
NSForegroundColorAttributeName: UIColor.green,
NSFontAttributeName: UIFont(name: "Helvetica-Bold", size: 10)!
], for: .normal)
}
}
}
答案 2 :(得分:0)
在iOS 12上,似乎无法使用setBadgeTextAttributes
设置字体。这是基于this answer的解决方案:
extension UITabBar {
func setBadge(value: String?, at index: Int, withConfiguration configuration: TabBarBadgeConfiguration = TabBarBadgeConfiguration()) {
let existingBadge = subviews.first { ($0 as? TabBarBadge)?.hasIdentifier(for: index) == true }
existingBadge?.removeFromSuperview()
guard let tabBarItems = items,
let value = value else { return }
let itemPosition = CGFloat(index + 1)
let itemWidth = frame.width / CGFloat(tabBarItems.count)
let itemHeight = frame.height
let badge = TabBarBadge(for: index)
badge.frame.size = configuration.size
badge.center = CGPoint(x: (itemWidth * itemPosition) - (0.5 * itemWidth) + configuration.centerOffset.x,
y: (0.5 * itemHeight) + configuration.centerOffset.y)
badge.layer.cornerRadius = 0.5 * configuration.size.height
badge.clipsToBounds = true
badge.textAlignment = .center
badge.backgroundColor = configuration.backgroundColor
badge.font = configuration.font
badge.textColor = configuration.textColor
badge.text = value
addSubview(badge)
}
}
class TabBarBadge: UILabel {
var identifier: String = String(describing: TabBarBadge.self)
private func identifier(for index: Int) -> String {
return "\(String(describing: TabBarBadge.self))-\(index)"
}
convenience init(for index: Int) {
self.init()
identifier = identifier(for: index)
}
func hasIdentifier(for index: Int) -> Bool {
let has = identifier == identifier(for: index)
return has
}
}
class TabBarBadgeConfiguration {
var backgroundColor: UIColor = .red
var centerOffset: CGPoint = .init(x: 12, y: -9)
var size: CGSize = .init(width: 17, height: 17)
var textColor: UIColor = .white
var font: UIFont! = .systemFont(ofSize: 11) {
didSet { font = font ?? .systemFont(ofSize: 11) }
}
static func construct(_ block: (TabBarBadgeConfiguration) -> Void) -> TabBarBadgeConfiguration {
let new = TabBarBadgeConfiguration()
block(new)
return new
}
}