【问题标题】:Changing the font style for UISearchBar cancel button更改 UISearchBar 取消按钮的字体样式
【发布时间】:2019-12-20 08:44:49
【问题描述】:

我正在尝试将 UISearchBar 取消按钮的字体更改为“OpenSans”,但我无法访问任何属性。我只能更改 UISearchBar 的 tint 颜色,这会更改搜索栏取消按钮文本和搜索栏 UITextField 光标颜色的颜色:

searchBar.tintColor = UIColor(red: 187.0/255.0, green: 187.0/255.0, blue: 187.0/255.0, alpha: 1.0)

有没有办法做到这一点?

【问题讨论】:

    标签: swift


    【解决方案1】:

    试试这样对我有用的东西。

     UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], forState: .Normal)
    

    如果您还想更改颜色,请将其添加到您的属性数组中。

     NSForegroundColorAttributeName : UIColor.whiteColor()
    

    注意:在 iOS 9 中可用

    【讨论】:

    • 你不会碰巧拥有 swift 等价物吧?
    • 漂亮,伙计。非常感谢。您可能需要注意,“appearanceWhenContainedInInstancesOfClasses”仅适用于 iOS 9.0 >
    • @JLO 我不确定你的意思,这很快。
    【解决方案2】:

    这适用于 Swift 3.0

    UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], for: .normal)
    

    【讨论】:

      【解决方案3】:

      迅速 2:

      if #available(iOS 9.0, *) {
           UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont.systemFontOfSize(15, weight: UIFontWeightLight)], forState: .Normal)
      } else {
           // Fallback on earlier versions
      }
      

      swift3:

      UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], for: .normal)
      

      目标-C:

      [[UIBarButtonItem appearanceWhenContainedIn:[UISearchBar class], nil] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                        [UIColor blueColor], 
                                                        UITextAttributeTextColor, 
                                                        [UIColor darkGrayColor], 
                                                        UITextAttributeTextShadowColor, 
                                                        [NSValue valueWithUIOffset:UIOffsetMake(0, -1)], 
                                                        UITextAttributeTextShadowOffset,
                                                        nil] 
                                              forState:UIControlStateNormal];
      

      【讨论】:

        【解决方案4】:

        Swift 4.2

        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSAttributedString.Key.font : UIFont(name: "your_font_name", size: 16)!], for: .normal)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          • 2013-10-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多