有沒有人有任何想法或代碼示例如何更改UISearchBar的佔位符文本的文本顏色?UISearchBar更改佔位符顏色
回答
爲iOS5+
使用外觀代理
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]];
由於我的文章我找到了答案,而且這確實是最好的辦法。乾淨,簡單,它不會破壞任何東西。 – csotiriou 2013-07-02 16:46:20
這不是真的有效。 UILabel不支持這種定製方式,因爲UILabel的'textColor'屬性沒有按照文檔的要求標記爲'UI_APPEARANCE_SELECTOR'。另請參見http://stackoverflow.com/questions/11839044/how-do-i-apply-uiappearance-proxy-properties-to-uilabel – 2013-07-22 02:23:42
只有當我在Stritboard中添加一些佔位符時,這才適用於我。直接設置佔位符通過代碼不工作,如果我們沒有在故事板中的一些佔位符..有線 – sach 2014-09-12 06:25:02
試試這個:
UITextField *searchField = [searchbar valueForKey:@"_searchField"];
field.textColor = [UIColor redColor]; //You can put any color here.
找到了答案從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"];
這是一個老的文章,但請查看這篇文章的一個適當的解決方案 iPhone UITextField - Change placeholder text color
試試這個:
[self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"];
您還可以設置這故事板,選擇搜索欄,在用戶自定義運行時添加條目屬性:
_searchField._placeholderLabel.textColor
型彩色的
,並選擇你需要的顏色。
第一個解決方案是可以的,但是如果你使用多個UISearchBar,或者創建了很多實例,它可能會失敗。在一個解決方案,總是對我的工作是也使用外觀代理,但直接的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];
這是唯一的解決方案,爲我工作正確,其他解決方案在來回導航viewController層次結構時失敗,並且銷燬/重新創建搜索欄 – DTs 2016-12-17 22:46:20
後調查了幾個答案,我出來這一點,希望其幫助
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;
}
}
}
我對UISearchBar類進行了子類化,然後使用了與您一樣的屬性佔位符。但在我的子類中,我添加了override var placeholder:String? {0} {0} {0} {0}這樣,無論何時我改變此searchBar上的佔位符文本,它都會自動使佔位符文本成爲正確的顏色。如果我沒有這樣做,那麼如果我設置了一個普通的佔位符,它會覆蓋我的佔位符顏色。 – 2017-01-29 01:59:44
這裏是一個解夫特:
夫特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
在你的viewController中的viewDidLoad將這個(iOS上的9個工作):
UITextField *searchField = [self.searchBar valueForKey:@"_searchField"]; //self.searchBar is mine targeted searchBar, replace with your searchBar reference
// Change search bar text color
searchField.textColor = [UIColor whiteColor];
// Change the search bar placeholder text color
[searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
//Change search bar icon if needed
[self.searchBar setImage:[UIImage imageNamed:@"searchIcon"]
forSearchBarIcon:UISearchBarIconSearch
state:UIControlStateNormal];
我已經從最近的答案中刪除了標語 - 在這裏的答案中沒有用處。謝謝! – Undo 2015-11-16 01:29:54
'[
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
}
斯威夫特3
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white
該解決方案適用於Xcode 8.2.1。與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
}
}
用例:
searchController.searchBar.setPlaceholderTextColorTo(color: mainColor)
這是一個老問題,但任何人現在絆腳石就可以了,你可以改變iOS版8.x的搜索圖標 - 10。使用以下3:
[_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
關於佔位符文本顏色,你可以檢查我的其他的答案,它採用了分類,在這裏:UISearchBar change placeholder color
- 1. UISearchBar佔位符文本顏色/位置
- 2. 更改UITextField佔位符顏色
- 3. 更改Braintree佔位符文本顏色
- 4. jQuery更改佔位符文本顏色
- 5. 如何更改輸入佔位符顏色javascript onmouseover,更改佔位符值onmouseover?
- 6. 佔位符顏色不會改變
- 7. 如何改變佔位符顏色CSS
- 8. Firefox的佔位符顏色改變
- 9. UISearchbar佔位符錯位
- 10. 更改佔位符的最後一個字符的顏色
- 11. 更改UISearchbar放大鏡顏色,PlaceHolder顏色和X顏色
- 12. 更改內部類佔位符的文本顏色
- 13. 在Javascript中更改佔位符的顏色以適用於Chrome
- 14. 如何更改佔位符文本的顏色
- 15. 如何更改javascript格式佔位符的顏色?
- 16. 如何更改目標c佔位符的顏色
- 17. 在動畫中更改佔位符顏色
- 18. 更改digitalBush/jquery.maskedinput中佔位符的顏色
- 19. CSS - 無法通過分類更改佔位符顏色
- 20. 角度材質2 - 更改md-select佔位符顏色
- 21. 無法更改輸入佔位符的顏色
- 22. 在窗體中更改下拉框佔位符的顏色
- 23. 如何更改默認的select2佔位符顏色的CSS?
- 24. 使用Userattributes更改UITextField的佔位符文本顏色?
- 25. 如何使用javascript更改佔位符的顏色?
- 26. 在驗證文本字段時更改佔位符顏色
- 27. 更改佔位符的顏色在下拉
- 28. 如何更改UITextField的佔位符顏色?
- 29. 如何更改焦點上的佔位符顏色?
- 30. 如何在離子輸入中更改佔位符的顏色?
我認爲這篇文章將幫助你的http://計算器。 com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – frankWhite 2012-08-06 14:52:11
我已經看過它。這篇文章涉及一個UITextfield。子類化UITextfield並重寫-drawPlaceholderInRect方法肯定會有所幫助。但是,UISearchBar不是UITextfield的子類,因此我無法重寫此方法。 – csotiriou 2012-08-06 20:25:24
對不起,我不知道這個。 UISearchBar是UIView的子類,我認爲你可以從它的子類中嘗試重寫drawRect方法,並在其中添加上一篇文章中的代碼。也許這有幫助 – frankWhite 2012-08-07 06:27:08