2012-08-06 28 views
33

क्या कोई भी कोई विचार या कोड नमूना है कि मैं UISearchBar के प्लेसहोल्डर टेक्स्ट का टेक्स्ट रंग कैसे बदल सकता हूं?UISearchBar प्लेसहोल्डर रंग बदलें

+0

मुझे लगता है कि इस पोस्ट आप http में मदद मिलेगी: // stackoverflow। कॉम/प्रश्न/1340224/iphone-uitextfield-change-placeholder-text-color – frankWhite

+0

मैंने इसे पहले से ही देखा है। यह पोस्ट यूआईटीएक्स्टफील्ड से संबंधित है। UITextfield को उपclassing और -drawPlaceholderInRect विधि को ओवरराइड करना निश्चित रूप से मदद करेगा। हालांकि, UISearchBar UITextfield का उप-वर्ग नहीं है, और इस तरह, मैं इस विधि को ओवरराइड नहीं कर सकता। – csotiriou

+0

क्षमा करें, मुझे यह नहीं पता। UISearchBar UIView का उप-वर्ग है, और मुझे लगता है कि आप इससे उपclass कर सकते हैं और drawRect विधि को ओवरराइड करने का प्रयास कर सकते हैं, इसे पिछले पोस्ट से कोड जोड़ना। शायद यह मदद – frankWhite

उत्तर

70

से मिले जवाब उपस्थिति प्रॉक्सी

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

पर बहुत अच्छा काम कर रहा है क्योंकि मेरी पोस्ट के बाद से मुझे जवाब मिल गया था, और वास्तव में यह करने का सबसे अच्छा तरीका है। स्वच्छ, सरल और यह कुछ भी तोड़ नहीं है। – csotiriou

+6

यह वास्तव में मान्य नहीं है। UILabel इस तरह अनुकूलन का समर्थन नहीं करता है, क्योंकि UILabel की 'textColor' प्रॉपर्टी को 'UI_APPEARANCE_SELECTOR' के साथ चिह्नित नहीं किया गया है, जैसा कि दस्तावेज़ द्वारा आवश्यक है। यह भी देखें http://stackoverflow.com/questions/11839044/how-do-i-apply-uiappearance-proxy-properties-to-uilabel –

+0

यह केवल मेरे लिए काम कर रहा है अगर मैं स्ट्रॉयरबोर्ड में कुछ प्लेसहोल्डर जोड़ता हूं .. सीधे प्लेसहोल्डर सेट करना कोड के माध्यम से काम नहीं करता है अगर हमारे पास स्टोरीबोर्ड में कुछ प्लेसहोल्डर नहीं है .. वायर्ड – sach

-2

इस प्रयास करें:

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

मैं रंग बदलना नहीं चाहता सामान्य पाठ मैं प्लेसहोल्डर टेक्स्ट का रंग बदलना चाहता हूं। – csotiriou

+4

यह ठीक नहीं है! जब आप ऐसा करते हैं तो आप एक निजी आईवीआर तक पहुंच रहे हैं और इससे ऐप स्टोर से आपका ऐप खारिज हो सकता है। –

+0

@AlexanderW: यदि इसका उपयोग नहीं किया गया था, तो आप इसे एक्सेस नहीं कर पाएंगे ... दुनिया भर में हर कोई टेक्स्टफील्ड प्लेसहोल्डर रंग परिवर्तन के रंग को बदलने के लिए इस चीज़ का उपयोग करता है .... मेरा ऐप कभी अस्वीकार नहीं किया गया है इसके कारण ... लेकिन यह सिर्फ एक समाधान है, एक आदर्श नहीं हो सकता ..... – Ankit

28

iOS5+ के लिए 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

+2

हालांकि यह कोड काम करता है, मैं इसके साथ किसी ऐप सबमिशन पर भरोसा करता हूं, क्योंकि यह ऐप्पल के नियमों का उल्लंघन करता है। – csotiriou

+0

मेरा ऐप इस समाधान के साथ एपस्टोर में सबमिट करना ठीक है। :) – Senry

3

इस प्रयास करें:

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

तुम भी स्टोरीबोर्ड, चयन खोज पट्टी में इस सेट कर सकते हैं, उपयोगकर्ता निर्धारित क्रम के तहत प्रविष्टि जोड़ने के गुण:

_searchField._placeholderLabel.textColor 
प्रकार रंग की

और चुनें रंग आपको चाहिए

15

पहला समाधान ठीक है, लेकिन यदि आप एकाधिक UISearchBar का उपयोग करते हैं, या बहुत से उदाहरण बनाते हैं तो यह असफल हो सकता है। एक ही समाधान है कि हमेशा मेरे लिए काम करने के बाद जवाब के एक जोड़े का सर्वेक्षण किया, मैं इस बाहर आते हैं, आशा है कि इसकी मदद

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

पर काम नहीं करता है यह एकमात्र समाधान है जो मेरे लिए सही तरीके से काम करता है, दृश्य समाधान नेविगेट करते समय अन्य समाधान विफल रहे, और खोज बार को नष्ट/पुनर्निर्मित करने के लिए – DTs

0

पर सीधे है स्विफ्ट के लिए समाधान:

स्विफ्ट 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

मैंने यूआईएसearchBar क्लास को उप-वर्गीकृत किया, और फिर मैंने आपके द्वारा किए गए एक जिम्मेदार प्लेसहोल्डर का उपयोग किया। लेकिन मेरे उप-वर्ग में, मैंने var प्लेसहोल्डर ओवरराइड जोड़ा: स्ट्रिंग? { didSet { changePlaceholdercolor (str: प्लेसहोल्डर ?? "") } }, इस तरह, जब भी मैं इस खोज पर प्लेसहोल्डर टेक्स्ट बदलता हूं, यह स्वचालित रूप से प्लेसहोल्डर को सही रंग टेक्स्ट बना देगा। अगर मैंने ऐसा नहीं किया है, तो यह मेरे प्लेसहोल्डर रंग को ओवरराइड करेगा यदि मैंने कभी भी सामान्य प्लेसहोल्डर सेट किया है। –

12

यहाँ एक है भी उपस्थिति प्रॉक्सी का उपयोग करने लेकिन 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

क्या यह सार्वजनिक एपीआई है? – Andrey

+0

मुझे ऐसा लगता है। यह वही समाधान है जो अन्य उपयोगकर्ता अनुशंसा करते हैं। – derdida

+0

'प्रॉम्प्ट' के बारे में कैसे ?! (प्लेसहोल्डर नहीं) – fatuhoku

0

अपने ViewController की viewDidLoad में इस रखो (आईओएस 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

+0

'[ setValue: forUndefinedKey:]: यह वर्ग कुंजी _placeholderLabel.textColor के लिए महत्वपूर्ण मूल्य कोडिंग-अनुरूप नहीं है।' –

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

यह समाधान पर काम करता है एक्सकोड 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) 
0

यह एक पुरानी सवाल है, लेकिन आजकल इस पर ठोकर किसी के लिए, आप आईओएस 8.x पर खोज आइकन बदल सकते हैं - 10।3 निम्नलिखित का उपयोग कर:

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

प्लेसहोल्डर पाठ रंग के बारे में, आप मेरे दूसरे सवाल का जवाब है, जो यहाँ एक श्रेणी का उपयोग करता है, जाँच कर सकते हैं: UISearchBar change placeholder color

संबंधित मुद्दे