2012-08-06 110 views
33

有沒有人有任何想法或代碼示例如何更改UISearchBar的佔位符文本的文本顏色?UISearchBar更改佔位符顏色

+0

我認爲這篇文章將幫助你的http://計算器。 com/questions/1340224/iphone-uitextfield-change-placeholder-text-color – frankWhite 2012-08-06 14:52:11

+0

我已經看過它。這篇文章涉及一個UITextfield。子類化UITextfield並重寫-drawPlaceholderInRect方法肯定會有所幫助。但是,UISearchBar不是UITextfield的子類,因此我無法重寫此方法。 – csotiriou 2012-08-06 20:25:24

+0

對不起,我不知道這個。 UISearchBar是UIView的子類,我認爲你可以從它的子類中嘗試重寫drawRect方法,並在其中添加上一篇文章中的代碼。也許這有幫助 – frankWhite 2012-08-07 06:27:08

回答

70

iOS5+使用外觀代理

[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setTextColor:[UIColor redColor]]; 
+0

由於我的文章我找到了答案,而且這確實是最好的辦法。乾淨,簡單,它不會破壞任何東西。 – csotiriou 2013-07-02 16:46:20

+6

這不是真的有效。 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

+0

只有當我在Stritboard中添加一些佔位符時,這才適用於我。直接設置佔位符通過代碼不工作,如果我們沒有在故事板中的一些佔位符..有線 – sach 2014-09-12 06:25:02

-2

試試這個:

UITextField *searchField = [searchbar valueForKey:@"_searchField"]; 
    field.textColor = [UIColor redColor]; //You can put any color here. 
+0

我不想改變顏色正常的文字。我想更改佔位符文本的顏色。 – csotiriou 2012-08-06 14:08:24

+4

這不好!當您這樣做時,您正在訪問PRIVATE iVAR,這可能會導致您的應用在應用商店被拒絕。 – 2014-05-19 11:06:17

+0

@AlexanderW:如果這意味着不能被使用,你將無法訪問它...全世界的每個人都使用這個東西來改變textfield佔位符顏色變化的顏色....我的應用程序從未被拒絕由於這...但它只是一個解決方案,可能不是一個完美的...... – Ankit 2014-05-23 09:54:06

28

找到了答案從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"]; 
+0

任何人都試圖提交一個應用程序的代碼?應該不是什麼大問題......但誰肯定可以這麼說... – jimpic 2013-04-10 07:17:07

+2

雖然這段代碼有效,但我相信任何提交的應用都會違反蘋果的規定。 – csotiriou 2013-06-10 17:20:24

+0

我的應用程序可以通過此解決方案提交給appstore。 :) – Senry 2013-08-28 11:31:02

3

試試這個:

[self.searchBar setValue:[UIColor whatever] forKeyPath:@"_searchField._placeholderLabel.textColor"]; 

您還可以設置這故事板,選擇搜索欄,在用戶自定義運行時添加條目屬性:

_searchField._placeholderLabel.textColor 
型彩色

,並選擇你需要的顏色。

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 appearanceWhenContainedIn:[UISearchBar class], nil] setAttributedPlaceholder:attributedPlaceholder]; 
+0

這是唯一的解決方案,爲我工作正確,其他解決方案在來回導航viewController層次結構時失敗,並且銷燬/重新創建搜索欄 – DTs 2016-12-17 22:46:20

0

後調查了幾個答案,我出來這一點,希望其幫助

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; 
     } 
    } 
} 
+0

我對UISearchBar類進行了子類化,然後使用了與您一樣的屬性佔位符。但在我的子類中,我添加了override var placeholder:String? {0} {0} {0} {0}這樣,無論何時我改變此searchBar上的佔位符文本,它都會自動使佔位符文本成爲正確的顏色。如果我沒有這樣做,那麼如果我設置了一個普通的佔位符,它會覆蓋我的佔位符顏色。 – 2017-01-29 01:59:44

12

這裏是一個解夫特:

夫特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 
+0

它是公共API嗎? – Andrey 2015-07-31 00:43:10

+0

我認爲如此。它與其他用戶推薦的解決方案相同。 – derdida 2015-07-31 07:08:37

+0

'提示符'怎麼樣?! (不是佔位符) – fatuhoku 2015-11-20 22:25:27

0

在你的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]; 
+3

我已經從最近的答案中刪除了標語 - 在這裏的答案中沒有用處。謝謝! – Undo 2015-11-16 01:29:54

+0

'[ setValue:forUndefinedKey:]:該類不是關鍵字_placeholderLabel.textColor的編碼兼容密鑰值。' – 2016-10-27 05:33:24

6
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 
} 
0

斯威夫特3

UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).textColor = UIColor.white 
0

該解決方案適用於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) 
0

這是一個老問題,但任何人現在絆腳石就可以了,你可以改變iOS版8.x的搜索圖標 - 10。使用以下3

[_searchBar setImage:[UIImage imageNamed:@"your-image-name"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal]; 

關於佔位符文本顏色,你可以檢查我的其他的答案,它採用了分類,在這裏:UISearchBar change placeholder color

相關問題