Изменить шрифт значка UITabBarItem в iOS 12 - PullRequest
0 голосов
/ 20 января 2019

Я хочу изменить свой шрифт значка tabbarItem, и я попытался сделать это, используя этот ответ , но, как говорится в комментариях, он работает для iOS 10, но в iOS 11 есть проблемы. Я пытаюсь сделайте это в iOS 12, используя swift 4, но это не работает.

UITabBarItem.appearance().setBadgeTextAttributes([NSAttributedStringKey.font.rawValue: UIFont(name: "IRANSans-Bold", size: 14) as Any], for: .normal)

Пожалуйста, дайте мне знать, если есть что сделать, или мне нужно создать специальный класс для этого.

1 Ответ

0 голосов
/ 21 января 2019

Я много искал и обнаружил, что невозможно изменить шрифт значка элемента панели вкладок. Итак, я вышел с написанием расширения для UITabBarController следующим образом:

    func setCustomBadge(){
    removeMyCustomBadge(view: self.tabBar)
    setBadgeLabel(view: self.tabBar)
}
func setBadgeLabel(view:UIView){
    for subview in (view.subviews){
        let myType = String(describing: type(of: subview))
        print(myType)
        if myType == "_UIBadgeView" {
          let customBadeg : UILabel = UILabel(frame: CGRect(x: subview.frame.origin.x, y: subview.frame.origin.y, width: subview.frame.size.width, height: subview.frame.size.height))
            customBadeg.font = UIFont(name: "IRANSans", size: 10)
            customBadeg.layer.masksToBounds = true
            customBadeg.layer.cornerRadius = customBadeg.frame.size.height/2
            customBadeg.textAlignment = .center
            customBadeg.textColor = UIColor.white
            customBadeg.backgroundColor = UIColor(named: "Red")
            customBadeg.text = Utility().enNums2Fa(inputString: String(format: "%d", Utility().retrieveUserBasketBadge()))
            view.addSubview(customBadeg)
            subview.isHidden = true
        }
        else {
            setBadgeLabel(view:subview)
        }
    }
  }
} 
...