Zay*_*ige 76
对于iOS5+
使用外观代理
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
Run Code Online (Sandbox Code Playgroud)
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)
小智 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)
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)
一条线解决方案
试试这个看看:(我用 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)
这是结果:
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)
斯威夫特 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)
快乐编码:)
在iOS 13上为我工作
searchBar.searchTextField.attributedPlaceholder = NSAttributedString(
string: "Search something blabla",
attributes: [.foregroundColor: UIColor.red]
)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
35482 次 |
最近记录: |