Hat jemand eine Idee oder ein Codebeispiel darüber, wie ich die Textfarbe des Platzhaltertexts einer UISearchBar ändern kann?
für iOS5+
verwenden Sie den Darstellungs-Proxy
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
Antwort gefunden von Ändern Sie die Platzhaltertextfarbe von UITextField programmgesteuert
// 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"];
Die erste Lösung ist in Ordnung, aber wenn Sie mehrere UISearchBar-Objekte verwenden oder viele Instanzen erstellen, kann dies fehlschlagen. Die eine Lösung, die immer für mich funktioniert, besteht darin, auch das Erscheinungs-Proxy zu verwenden, aber direkt auf UITextField
NSDictionary *placeholderAttributes = @{
NSForegroundColorAttributeName: [UIColor darkButtonColor],
NSFontAttributeName: [UIFont fontWithName:@"HelveticaNeue" size:15],
};
NSAttributedString *attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.searchBar.placeholder
attributes:placeholderAttributes];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setAttributedPlaceholder:attributedPlaceholder];
Hier ist eine Lösung für Swift:
Swift 2
var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
var textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.whiteColor()
Swift 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
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
}
Versuchen Sie dies und sehen Sie: (Ich habe Code mit Swift 4.1 getestet - 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
}
}
Hier ist das Ergebnis:
Swift 3
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white
Versuche dies:
[self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"];
Sie können dies auch im Storyboard festlegen, die Suchleiste auswählen und unter Benutzerdefinierte Laufzeitattribute einen Eintrag hinzufügen:
_searchField._placeholderLabel.textColor
vom Typ Color und wählen Sie die gewünschte Farbe.
Es ist ein alter Beitrag, aber überprüfen Sie diesen Beitrag auf eine geeignete Lösung iPhone UITextField - Platzhaltertextfarbe ändern
Nachdem ich ein paar Antworten befragt habe, komme ich hier raus, ich hoffe, es hilft
for (UIView *subview in searchBar.subviews) {
for (UIView *sv in subview.subviews) {
if ([NSStringFromClass([sv class]) isEqualToString:@"UISearchBarTextField"]) {
if ([sv respondsToSelector:@selector(setAttributedPlaceholder:)]) {
((UITextField *)sv).attributedPlaceholder = [[NSAttributedString alloc] initWithString:searchBar.placeholder attributes:@{NSForegroundColorAttributeName: [UIColor whiteColor]}];
}
break;
}
}
}
Diese Lösung funktioniert mit Xcode 8.2.1. mit Swift 3.0.:
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
}
}
Anwendungsbeispiel:
searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)