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

How to set change placeholder color on searchbar?

extension UISearchBar {

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

    let svs = subviews.flatMap { [=10=].subviews }
    guard let element = (svs.filter { [=10=] 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)

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

避免这种情况的一种简单方法是从更新占位符属性的单个入口点开始,颜色和字体 setter 方法都使用该属性。我使用了一个计算 属性,它同时具有 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 扩展(我会用计算的 属性 textField 替换 getSearchBarTextField 方法),我们可以删除对属性字符串的任何引用等等

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