Swift - 如何更改 UITabBarItem 徽章字体



我有一个UITabBar .我为选项卡栏项目设置了一个值:

tabBarItem3?.badgeValue = String(self.numberOfProducts)

现在我想将其字体更改为特定字体。然后我使用了这段代码:

tabBarItem3?.setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .normal)

它不起作用。我该怎么办?

Swift 3

UITabBarItem.appearance().setBadgeTextAttributes([.font: UIFont.systemFont(ofSize: 30, weight: .medium)], for: .normal)
UITabBarItem.appearance().setBadgeTextAttributes([.font: UIFont.systemFont(ofSize: 30, weight: .medium)], for: .selected)

UIKit 会在视图layoutSubviewsviewWillAppear后的某个时间更新徽章字体。完全覆盖这将需要一些代码。您想从观察徽章的字体变化开始。

tabBarItem.addObserver(self, forKeyPath: "view.badge.label.font", options: .new, context: nil)

现在,一旦调用了观察方法,就可以安全地设置徽章字体。然而,有一个问题。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中。完成后也不要忘记删除观察者!

试试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)
      }
    }
  }
}
<</div> div class="one_answers">

适用于 iOS15+

let tabBarAppearance: UITabBarAppearance = UITabBarAppearance()
tabBarAppearance.configureWithOpaqueBackground()
tabBarAppearance.stackedLayoutAppearance.normal.badgeTextAttributes = [
    .font: UIFont.systemFont(ofSize: 11)
]
// also can change title:
// tabBarAppearance.stackedLayoutAppearance.normal.titleTextAttributes = [...]
// tabBarAppearance.stackedLayoutAppearance.selected.titleTextAttributes = [...]
UITabBar.appearance().standardAppearance = tabBarAppearance
UITabBar.appearance().scrollEdgeAppearance = tabBarAppearance

适用于 iOS14 及更低版本

UITabBarItem.appearance().setBadgeTextAttributes([
            .font: UIFont.systemFont(ofSize: 11)
        ], for: .normal)

Swift 3

UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 10)], for: .normal)
UITabBarItem.appearance().setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 10)], for: .selected)

最新更新