UISearchBar更改占位符颜色

cso*_*iou 42 placeholder uisearchbar ios

有没有任何想法或代码示例如何更改UISearchBar的占位符文本的文本颜色?

Zay*_*ige 76

对于iOS5+使用外观代理

[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)

  • 这不是真的有效.UILabel不支持这种方式的自定义,因为UILabel的`textColor`属性没有按照文档的要求标记为`UI_APPEARANCE_SELECTOR`.另见http://stackoverflow.com/questions/11839044/how-do-i-apply-uiappearance-proxy-properties-to-uilabel (9认同)
  • 我偶然给了这个东西投票.它不好用.人们应该使用:http://stackoverflow.com/questions/1340224/iphone-uitextfield-change-placeholder-text-color (7认同)
  • 这不适用于Swift代码,仅适用于Objective-C. (3认同)

Way*_*Liu 30

编程方式Change UITextField的占位符文本颜色中找到答案

// Get the instance of the UITextField of the search bar
UITextField *searchField = [searchBar valueForKey:@"_searchField"];

// Change search bar text color
searchField.textColor = [UIColor redColor];

// Change the search bar placeholder text color
[searchField setValue:[UIColor blueColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

  • 我担心这会突然开始崩溃iOS更新. (7认同)
  • 虽然这段代码有效,但我相信任何提交的应用程序,因为它违反了Apple的规定. (2认同)
  • 在iOS8.0上崩溃 (2认同)

小智 15

第一种解决方案是可以的,但是如果你使用多个UISearchBar,或者创建了很多实例,它可能会失败.总是对我有用的一个解决方案是使用外观代理但直接在UITextField上使用

   NSDictionary *placeholderAttributes = @{
                                            NSForegroundColorAttributeName: [UIColor darkButtonColor],
                                            NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15],
                                            };

    NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.searchBar.placeholder
                                                                                attributes:placeholderAttributes];

    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setAttributedPlaceholder:attributedPlaceholder];
Run Code Online (Sandbox Code Playgroud)


der*_*ida 15

这是Swift的解决方案:

斯威夫特2

var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()

var textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.whiteColor()
Run Code Online (Sandbox Code Playgroud)

斯威夫特3

let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.white

let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.white
Run Code Online (Sandbox Code Playgroud)

  • 适用于iOS 10.2.1 (2认同)

Suc*_*ija 14

从 iOS 13.0 开始,这很容易,您可以简单地使用搜索栏的searchTextField属性来更新占位符的属性。

self.searchController.searchBar.searchTextField.attributedPlaceholder =  NSAttributedString.init(string: "Search anything...", attributes: [NSAttributedString.Key.foregroundColor:UIColor.red])
Run Code Online (Sandbox Code Playgroud)

一条线解决方案


Kru*_*nal 9

试试这个看看:(我用 Swift 4.1 - Xcode 9.3-beta4测试了下面的代码)

@IBOutlet weak var sbSearchBar: UISearchBar!

if let textfield = sbSearchBar.value(forKey: "searchField") as? UITextField {

    textfield.backgroundColor = UIColor.yellow
    textfield.attributedPlaceholder = NSAttributedString(string: textfield.placeholder ?? "", attributes: [NSAttributedStringKey.foregroundColor : UIColor.red])

    textfield.textColor = UIColor.green

    if let leftView = textfield.leftView as? UIImageView {
        leftView.image = leftView.image?.withRenderingMode(.alwaysTemplate)
        leftView.tintColor = UIColor.red
    }
}
Run Code Online (Sandbox Code Playgroud)

这是结果:

在此处输入图片说明


phi*_*sch 7

if let textFieldInsideSearchBar = searchBar.value(forKey: "searchField") as ? UITextField {
    textFieldInsideSearchBar ? .textColor = UIColor.white

    if let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as ? UILabel {
        textFieldInsideSearchBarLabel ? .textColor = UIColor.white

        if let clearButton = textFieldInsideSearchBar ? .value(forKey: "clearButton") as!UIButton {

            clearButton.setImage(clearButton.imageView ? .image ? .withRenderingMode(.alwaysTemplate),
                for : .normal)
            clearButton.tintColor = UIColor.white
        }
    }

    let glassIconView = textFieldInsideSearchBar ? .leftView as ? UIImageView

    glassIconView ? .image = glassIconView ? .image ? .withRenderingMode(.alwaysTemplate)
    glassIconView ? .tintColor = UIColor.white
}
Run Code Online (Sandbox Code Playgroud)


rup*_*h45 6

斯威夫特 5 - iOS 13

那些被卡住的人让我告诉你它只能在 viewDidLayoutSubviews 中工作,而不能在 viewDidLoad 中工作

override func viewDidLayoutSubviews() {

    setupSearchBar(searchBar: YourSearchBar)

}

    func setupSearchBar(searchBar : UISearchBar) {

    searchBar.setPlaceholderTextColorTo(color: UIColor.white)        

   }

extension UISearchBar
{
    func setPlaceholderTextColorTo(color: UIColor)
    {
        let textFieldInsideSearchBar = self.value(forKey: "searchField") as? UITextField
        textFieldInsideSearchBar?.textColor = color
        let textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.value(forKey: "placeholderLabel") as? UILabel
        textFieldInsideSearchBarLabel?.textColor = color
    }
}
Run Code Online (Sandbox Code Playgroud)

快乐编码:)


and*_*rei 5

在iOS 13上为我工作

searchBar.searchTextField.attributedPlaceholder = NSAttributedString(
    string: "Search something blabla",
    attributes: [.foregroundColor: UIColor.red]
)
Run Code Online (Sandbox Code Playgroud)