有没有人对我怎样才能改变一个UISearchBar的占位符文本的文本颜色的任何想法或示例代码?
Answer 1:
对于iOS5+
使用外观代理
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
Answer 2:
发现从应答变化的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"];
Answer 3:
下面是斯威夫特的解决方案:
斯威夫特2
var textFieldInsideSearchBar = searchBar.valueForKey("searchField") as? UITextField
textFieldInsideSearchBar?.textColor = UIColor.whiteColor()
var textFieldInsideSearchBarLabel = textFieldInsideSearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideSearchBarLabel?.textColor = UIColor.whiteColor()
斯威夫特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
Answer 4:
第一个解决方案是好的,但如果你使用多个的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];
Answer 5:
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
}
Answer 6:
斯威夫特3
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white
Answer 7:
试试这个,看看(我测试下面的代码与雨燕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
}
}
下面是结果:
Answer 8:
试试这个:
[self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"];
您还可以设置在这个故事板,选择搜索栏,在用户自定义运行属性添加条目:
_searchField._placeholderLabel.textColor
的类型颜色 ,然后选择您需要的颜色。
Answer 9:
这是一个老的文章,但请查看这篇文章的一个合适的解决方案iPhone的UITextField -更改占位符文本颜色
Answer 10:
接受调查的一对夫妇的答案后,我走出这一点,希望它的帮助
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;
}
}
}
Answer 11:
该解决方案适用于Xcode的8.2.1。 斯威夫特与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
}
}
用法示例:
searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
Answer 12:
这是一个老问题,但任何人现在绊脚石就可以了,你可以改变在iOS 8.x中的搜索图标-使用下面的10.3:
[_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
关于占位符文本颜色,你可以检查我的其他的答案,它采用了类别,此处的UISearchBar变化占位符颜色
Answer 13:
试试这个:
UITextField *searchField = [searchbar valueForKey:@"_searchField"];
field.textColor = [UIColor redColor]; //You can put any color here.
文章来源: UISearchBar change placeholder color