如何在搜索栏上设置更改占位符的颜色?

时间:2019-01-27 15:05:06

标签: ios swift uisearchbar

extension UISearchBar {

private func getViewElement<T>(type: T.Type) -> T? {

    let svs = subviews.flatMap { $0.subviews }
    guard let element = (svs.filter { $0 is T }).first as? T else { return nil }
    return element
}

func getSearchBarTextField() -> UITextField? {

    return getViewElement(type: UITextField.self)
}

func setTextColor(color: UIColor) {

    if let textField = getSearchBarTextField() {
        textField.textColor = color
    }
}

func setTextFieldColor(color: UIColor) {

    if let textField = getViewElement(type: UITextField.self) {
        switch searchBarStyle {
        case .minimal:
            textField.layer.backgroundColor = color.cgColor
            textField.layer.cornerRadius = 6

        case .prominent, .default:
            textField.backgroundColor = color
        }
    }
}

func setPlaceholderTextColor(color: UIColor) {

    if let textField = getSearchBarTextField() {
        textField.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "", attributes: [NSAttributedString.Key.foregroundColor:color])
    }
}

func setPlaceholderfont(fontfamily: UIFont) {

    if let textField = getSearchBarTextField() {
        textField.attributedPlaceholder = NSAttributedString(string: self.placeholder != nil ? self.placeholder! : "", attributes: [NSAttributedString.Key.font:fontfamily])
    }
}


 }

我为Uisearchbbar创建了一个扩展名,以便能够自定义占位符

在扩展名 setPlaceholderfont()中运行正常

当我调用以下方法时,占位符文本的颜色不变

pick.searchBar.setPlaceholderTextColor(color: Server().primarycolor)

1 个答案:

答案 0 :(得分:0)

调用两种方法更新属性占位符的颜色和字体时,您将用最后一种覆盖。因此,假设您先设置颜色,然后再设置字体,则在设置字体属性时会删除color属性。

避免这种情况的一种简单方法是从一个单一的入口点开始,该入口点将更新占位符属性,颜色和字体设置方法都使用该占位符。我使用了同时具有getter和setter的计算属性-它可以获取和设置占位符的当前属性。

extension UITextField {
    var placeholderColor: UIColor? {
        get { return placeholderAttributes?[.foregroundColor] as? UIColor }
        set { placeholderAttributes?[.foregroundColor] = newValue }
    }

    var placeholderFont: UIFont? {
        get { return placeholderAttributes?[.font] as? UIFont }
        set { placeholderAttributes?[.font] = newValue }
    }

    var placeholderAttributes: [NSAttributedString.Key: Any]? {
        get { return attributedPlaceholder?.attributes(at: 0, effectiveRange: nil) }
        set { attributedPlaceholder = .init(string: placeholder ?? "", attributes: newValue) }
    }
}

因此,切回您的UISearchBar扩展名(我将getSearchBarTextField方法替换为计算属性textField),我们可以删除对属性字符串等的任何引用。

var textField: UITextField? {
    return getViewElement(type: UITextField.self)
}

func setTextColor(color: UIColor) {
    textField?.textColor = color
}

func setPlaceholderTextColor(color: UIColor) {
    textField?.placeholderColor = color
}

func setPlaceholderFont(font: UIFont) {
    textField?.placeholderFont = font
}

尽管在某些情况下,具有适当类型(UIColorUIFont)的属性可能会派上用场,但从技术上讲,您不需要placeholderColorplaceholderFont属性,因为您可以使用扩展名中的文本字段placeholderAttributes设置它们。

extension UITextField {
    var placeholderAttributes: [NSAttributedString.Key: Any]? {
        get { return attributedPlaceholder?.attributes(at: 0, effectiveRange: nil) }
        set { attributedPlaceholder = .init(string: placeholder ?? "", attributes: newValue) }
    }
}

extension UISearchBar {

    // ...

    func setPlaceholderTextColor(color: UIColor) {
        textField?.placeholderAttributes?[.foregroundColor] = color
    }

    func setPlaceholderfont(fontfamily: UIFont) {
        textField?.placeholderAttributes?[.font] = fontfamily
    }
}