Swift - How do I change the font of a UITabBarItem?

I have UITabBar

. I am setting value for tab bar items:

tabBarItem3?.badgeValue = String(self.numberOfProducts)

      

Now I want to change the font to a specific font. Then I used this code:

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

      

This does not work. What should I do?

+6


source to share


5 answers


Swift 3



UITabBarItem.appearance().setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .normal)
UITabBarItem.appearance().setBadgeTextAttributes([NSFontAttributeName: UIFont(name: "IRANSans", size: 14)], for: .selected)

      

+3


source


UIKit updates the font of the icon some time after viewing layoutSubviews

or viewWillAppear

. A bit of code is required to overcome this completely. You want to start by observing the icon font change.

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

      

Now that the observation method is said to be safe to install the icon font. However, there is one attempt. UIKit will not apply the same change twice. To work around this issue, first set the icon attributes to zero and then reapply the font.



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)
    }
}

      

By simply inserting future iOS updates, you can wrap addObserver

in try-catch. Also, don't forget to remove the observer when you're done!

+1


source


swift 4

    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 9)], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.font: UIFont.systemFont(ofSize: 9)], for: .selected)

      

0


source


Try two functions from 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):

   '''
   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)
      }
    }
  }

}

      

0


source


Swift 3

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

      

-3


source







All Articles