2012-07-09 15 views
5

Będę musiał pokazać UISearchBar w różnych pozycjach z powodu różnych języków wprowadzania.
Widziałem this answer, ale nie mogę znaleźć, gdzie jest to, że faktycznie wyrównanie tekstu w prawo na UISearchBar.Czy można zmienić wyrównanie elementu zastępczego UISearchBar?

Wszelkie pomysły?
Dzięki!

+0

do szybkiego 3. znalazłem rozwiązanie tutaj: [* * Dostosuj tekstfield łatwo **] (http://stackoverflow.com/a/40105165/4593553) – Jerome

Odpowiedz

6

To co zrobiłem:

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

Jeśli też chcesz przenieść ikonę lupy, można to zrobić:

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

Ale trzeba będzie zapewnić search_icon .png obraz.

+0

Btw, aby to działało z iOS 7 potrzebujesz: [[self.subviews objectAtIndex: 0] subviews] w dla pętli zamiast tylko self.subviews –

3

Tutaj jest rozwiązanie

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

Otrzymuję błąd "oczekiwano" w drugiej linii – SleepsOnNewspapers

+1

Brakuje dwukropka, uprzejmie połóż tht. –

+1

Zaktualizowano odpowiedź! –

0

zestaw przypisać zastępczy tekst wyrównany do lewej osiągnąć zastępczy, spróbuj tego kodu ...

//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; 
Powiązane problemy