2012-07-09 14 views
5

मुझे विभिन्न इनपुट भाषाओं की वजह से विभिन्न संरेखणों में UISearchBar दिखाने की आवश्यकता होगी।
मैंने this answer देखा, लेकिन मुझे यह नहीं पता कि यह वास्तव में UISearchBar पर दाईं ओर टेक्स्ट को संरेखित कर रहा है।UISearchBar प्लेसहोल्डर के संरेखण को बदलना संभव है?

कोई विचार?
धन्यवाद!

+0

स्विफ्ट 3. के लिए मैं यहाँ एक समाधान पाया है: [* * आसानी से टेक्स्टफील्ड को कस्टमाइज़ करें **] (http://stackoverflow.com/a/40105165/4593553) – Jerome

उत्तर

6

यह मैं क्या किया है है:

for (UIView *subView in self.subviews){ 
     if ([subView isKindOfClass:[UITextField class]]) { 
      UITextField *text = (UITextField*)subView; 
      text.textAlignment = UITextAlignmentRight; 
      text.rightViewMode = UITextFieldViewModeAlways; 
     } 
    } 

आप भी आवर्धक लेंस आइकन पर ले जाना चाहते हैं, तो आप ऐसा कर सकते हैं:

text.leftView = nil; 
text.rightView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search_icon.png"]]; 

लेकिन आप search_icon प्रदान करना होगा .png छवि।

+0

बीटीडब्ल्यू, आईओएस 7 के साथ काम करने के लिए आपको यह चाहिए: [[self.subviews objectAtIndex: 0] सबव्यूज़] सिर्फ self.subviews –

3

यहाँ समाधान

UITextField *searchTextField = [searchBar valueForKey:@"_searchField"]; 
UILabel *placeholderLabel = [searchTextField valueForKey:@"_placeholderLabel"]; 
[placeholderLabel setTextAlignment:NSTextAlignmentLeft]; 
+0

की बजाय लूप के लिए मुझे मिलता है दूसरी पंक्ति – SleepsOnNewspapers

+1

पर एक त्रुटि "अपेक्षित]" एक लापता कोलन है, कृपया कृपया tht। –

+1

उत्तर अपडेट किया गया! –

0

सेट जिम्मेदार ठहराया प्लेसहोल्डर पाठ, बाईं गठबंधन प्लेसहोल्डर को प्राप्त करने के लिए इस कोड की कोशिश ...

//Step1 : Make blank attributed string 
    NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:@""]; 

    //Step2 : Append placeholder to blank attributed string 
    NSString *strSearchHere = @"Search here..."; 
    NSDictionary * attributes = [NSDictionary dictionaryWithObject:[UIColor blackColor] forKey:NSForegroundColorAttributeName]; 
    NSAttributedString * subString = [[NSAttributedString alloc] initWithString:strSearchHere attributes:attributes]; 
    [attributedString appendAttributedString:subString]; 

    //Step3 : Append dummy text to blank attributed string 
    NSString *strLongText = @"LongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongTextLongText"; 
    NSDictionary * attributesLong = [NSDictionary dictionaryWithObject:[UIColor clearColor] forKey:NSForegroundColorAttributeName]; 
    NSAttributedString * subStringLong = [[NSAttributedString alloc] initWithString:strLongText attributes:attributesLong]; 
    [attributedString appendAttributedString:subStringLong]; 

    //Step4 : Set attributed placeholder string to searchBar textfield 
    UITextField *searchTextField = [searchBarr valueForKey:@"_searchField"]; 
    searchTextField.attributedPlaceholder = attributedString; 
    searchTextField.leftView = nil; //To Remove Search icon 
    searchTextField.textAlignment = NSTextAlignmentLeft; 
संबंधित मुद्दे