2012-07-09 14 views
5

Ich werde die UISearchBar aufgrund verschiedener Eingabesprachen in verschiedenen Alignments anzeigen müssen.
Ich sah this answer, aber ich kann nicht finden, wo es ist, dass Sie den Text auf einer UISearchBar tatsächlich rechts ausrichten.Ist es möglich, die Ausrichtung eines UISearchBar-Platzhalters zu ändern?

Irgendwelche Ideen?
Danke!

+0

Für Swift 3. ich eine Lösung finden Sie hier: [* * Textfield leicht anpassen **] (http://stackoverflow.com/a/40105165/4593553) – Jerome

Antwort

6

Dies ist, was ich getan habe:

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

Wenn Sie auch auf das Lupensymbol verschieben möchten, können Sie dies tun:

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

Aber Sie werden die search_icon zu bieten haben .png Bild. Hier

+0

Btw, um dies mit iOS 7 arbeiten zu lassen, benötigen Sie: [[selfnviews objectAtIndex: 0] Unteransichten] in der for-Schleife statt nur self_nviews –

3

ist die Lösung

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

ich bekomme ein Fehler "erwartet" in der zweiten Zeile – SleepsOnNewspapers

+1

Es gibt einen fehlenden Doppelpunkt, freundlicherweise thth. –

+1

Die Antwort wurde aktualisiert! –

0

Set Platzhalter-Text zugeschrieben linksbündig Platzhalter, versuchen Sie diesen Code zu erreichen ...

//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; 
Verwandte Themen