【问题标题】:Change UISearchBar textColor not working更改 UISearchBar textColor 不起作用
【发布时间】:2016-06-11 11:18:40
【问题描述】:

我正在使用 Google Places API 来实现自动完成小部件。我正在使用 swift 在 ios 9+ 中显示全屏控件。我正在添加文档中给出的完整控件。

我已经添加了文档中显示的代码。现在我想将 searchBar 文本颜色更改为 whiteColor。

所以我尝试了这个

UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()

但我没有得到想要的行为。下面是截图

这已在文档中给出

https://developers.google.com/places/ios-api/autocomplete#use_a_table_data_source

但这不起作用。我需要这方面的帮助。

【问题讨论】:

  • 解释所需的输出和您遇到的错误
  • 如果可能的话,最好详细说明问题并附上快照,否则很难得到准确的解决方案。

标签: google-maps uisearchbar google-places-api uiappearance


【解决方案1】:

你需要创建一个如下扩展:

public extension UISearchBar {

    public func setNewcolor(color: UIColor) {
        let clrChange = subviews.flatMap { $0.subviews }
        guard let sc = (clrChange.filter { $0 is UITextField }).first as? UITextField else { return }
        sc.textColor = color
    }
}

并使用以下代码更改颜色:

 controller.searchBar.setNewcolor(UIColor.redColor())

输出是:

更新:

要更改GMSAutocompleteViewController 的搜索栏文本颜色,您需要执行以下代码:

let searchBarTextAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

改变文字输出如下图:

如果您想更改占位符文本和searchBar 的颜色。您需要执行以下代码:

let placeholderAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]

let attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Find a place", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder

它会显示如下:

【讨论】:

    【解决方案2】:

    使用 Swift 3

    的波纹管代码
    if #available(iOS 9.0, *) {
      UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.green]
      } else {
      // Fallback on earlier versions
    }
    

    希望这会有所帮助。

    【讨论】:

      【解决方案3】:

      适用于 swift 4 ios 12

      UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes
                  .updateValue(UIColor.white, forKey: NSAttributedStringKey.foregroundColor.rawValue)
      

      【讨论】:

        【解决方案4】:

        您可以使用UIAppearance protocol 获取iOS 5.0 及更高版本中可用的类的外观代理。

        实际上有两种方法可以自定义对象的外观和获取类的外观代理。

        • 要自定义类的所有实例的外观,请使用appearance。
        • 要自定义包含在容器类实例或层次结构中的实例的类实例的外观,请使用appearanceWhenContainedIn。

        您可以应用此示例代码:

        [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];
        

        您还可以尝试此 SO 帖子中提供的另一个选项 - UISearchBar text color change in iOS 7。

        我希望这涵盖了您的问题。编码愉快!

        【讨论】:

        • 它不包括我的问题。我正在使用 swift,在 swift 中我们不能使用 defaulttext 属性。
        【解决方案5】:

        在 Swift 5 中:

        let searchBarTextAttributes: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 14.0)]
        
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
        

        【讨论】:

          【解决方案6】:

          这其实很简单,只要访问 searchBar 里面的 textField 就可以了:

          searchBar.searchTextField.defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-03-19
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-07-30
            • 2020-06-29
            • 2011-05-01
            • 1970-01-01
            相关资源
            最近更新 更多