2012-07-09 8 views
5

Avrò bisogno di mostrare la UISearchBar in diversi allineamenti a causa delle diverse lingue di input.
Ho visto this answer, ma non riesco a trovare dove si allinea effettivamente il testo a destra su un UISearchBar.È possibile modificare l'allineamento di un segnaposto di UISearchBar?

Qualche idea?
Grazie!

+0

Per Swift 3. ho trovato qui una soluzione: [* * Personalizza facilmente il campo di testo **] (http://stackoverflow.com/a/40105165/4593553) – Jerome

risposta

6

Questo è quello che ho fatto:

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

caso anche si desidera spostare l'icona della lente di ingrandimento, è possibile farlo:

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

Ma si dovrà fornire la search_icon immagine .png.

+0

Btw, per farlo funzionare con iOS 7 hai bisogno di: [[self.subviews objectAtIndex: 0] subviews] nel per loop invece di solo self.subviews –

3

Ecco la soluzione

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

ottengo un errore "atteso]" sulla seconda riga – SleepsOnNewspapers

+1

C'è un due punti mancante, gentilmente messo a posto. –

+1

Aggiornamento della risposta! –

0

Set attribuito testo segnaposto per ottenere allineato a sinistra segnaposto, provare questo code ...

//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; 
Problemi correlati