有没有人有想法或者代码示例,可以让我改变UISearchBar占位文本的文本颜色?
对于 iOS5+
使用外观代理
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
从Change UITextField's placeholder text color programmatically中找到答案。
// 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"];
prompt
参数怎么样? - fatuhoku第一种解决方案是可以的,但如果您使用多个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];
这里是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
prompt
怎么样?!(不是占位符) - fatuhokuiOS 13.0及以上版本很容易实现,您只需使用搜索栏的searchTextField属性即可更新占位符的属性。
self.searchController.searchBar.searchTextField.attributedPlaceholder = NSAttributedString.init(string: "Search anything...", attributes: [NSAttributedString.Key.foregroundColor:UIColor.red])
一行解决方案
试试这个,看看效果:(我用 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
}
}
这是结果:
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
}
Swift 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
}
}
愉快地编码 :)
在IOS 13上对我有用。
searchBar.searchTextField.attributedPlaceholder = NSAttributedString(
string: "Search something blabla",
attributes: [.foregroundColor: UIColor.red]
)
看起来苹果不想让我们在UISearchBar
类中玩弄占位符颜色。那么,让我们创建自己的占位符标签!
let searchBar = searchController.searchBar
// ...
// Configure `searchBar` if needed
// ...
let searchTextField: UITextField
if #available(iOS 13, *) {
searchTextField = searchBar.searchTextField
} else {
searchTextField = (searchBar.value(forKey: "searchField") as? UITextField) ?? UITextField()
}
// Since iOS 13 SDK, there is no accessor to get the placeholder label.
// This is the only workaround that might cause issued during the review.
if let systemPlaceholderLabel = searchTextField.value(forKey: "placeholderLabel") as? UILabel {
// Empty or `nil` strings cause placeholder label to be hidden by the search bar
searchBar.placeholder = " "
// Create our own placeholder label
let placeholderLabel = UILabel(frame: .zero)
placeholderLabel.text = "Search"
placeholderLabel.font = UIFont.systemFont(ofSize: 17.0, weight: .regular)
placeholderLabel.textColor = UIColor.blue.withAlphaComponent(0.5)
systemPlaceholderLabel.addSubview(placeholderLabel)
// Layout label to be a "new" placeholder
placeholderLabel.leadingAnchor.constraint(equalTo: systemPlaceholderLabel.leadingAnchor).isActive = true
placeholderLabel.topAnchor.constraint(equalTo: systemPlaceholderLabel.topAnchor).isActive = true
placeholderLabel.bottomAnchor.constraint(equalTo: systemPlaceholderLabel.bottomAnchor).isActive = true
placeholderLabel.translatesAutoresizingMaskIntoConstraints = false
placeholderLabel.setContentCompressionResistancePriority(.defaultHigh, for: .horizontal)
} else {
searchBar.placeholder = ""
}