2015-02-23 16 views
8

Attualmente non riesco ad allineare verticalmente un attributo Portaporta all'interno di un UITextField e non ho idea del perché.Centrare verticalmente un PortalPile attribuito di UITextField

Ecco quello che sto facendo:

self.addressBar = [[UITextField alloc] initWithFrame: CGRectMake(...)]; 
self.addressBar.backgroundColor = [UIColor whiteColor]; 
self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder text" 
           attributes:@{ 
              NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f], 
              NSFontAttributeName : [UIFont fontWithName:@"Gotham-BookItalic" size:14.0], 
              } 
]; 
self.addressBar.delegate = self; 
self.addressBar.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter; 
[self.view addSubview:self.addressBar]; 

Ed ecco cosa succede:

The UITextFieldLabel is smaller than its parent

E 'abbastanza chiaro che questo accade a causa del fatto che l'altezza della UITextFieldLabel è 10px più piccolo del UItextField stesso, ma non riesco a cambiarlo.

Questo accade solo utilizzando un attributoPlaceholder; la proprietà del segnaposto di default funziona bene.

Qualche idea?

+0

Il segnaposto predefinito ha le stesse dimensioni del font di un campo di testo. Ecco perché è centrato. – kean

+0

OK ha senso, ma non esiste un altro modo per centrare il segnaposto mantenendo le dimensioni del carattere? –

risposta

14

Usa NSParagraphStyle per aumentare altezza minima linea:

NSMutableParagraphStyle *style = [self.addressBar.defaultTextAttributes[NSParagraphStyleAttributeName] mutableCopy]; 
style.minimumLineHeight = self.addressBar.font.lineHeight - (self.addressBar.font.lineHeight - [UIFont fontWithName:@"Gotham-BookItalic" size:14.0].lineHeight)/2.0; 

self.addressBar.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"Placeholder text" 
          attributes:@{ 
             NSForegroundColorAttributeName: [UIColor colorWithRed:79/255.0f green:79/255.0f blue:79/255.0f alpha:0.5f], 
             NSFontAttributeName : [UIFont fontWithName:@"Gotham-BookItalic" size:14.0], 
             NSParagraphStyleAttributeName : style 
             } 
]; 

Si potrebbe anche sostituire un metodo - (CGRect)placeholderRectForBounds:(CGRect)bounds; in UITextField sottoclasse.

E 'disordinato, ma funziona :)

+1

Perfetto! Grazie :) Ti assegnerò la taglia non appena possibile (15h da ora) –

+0

Tutto fatto. Grazie ancora –

2

Segnaposto non sarà allineato se il carattere del testo del campo di testo è diverso da quello dei caratteri di segnaposto di campo di testo. Assicurati che entrambi i font siano uguali se vuoi allineare correttamente oppure puoi seguire la risposta di Kean.

textField.font = [UIFont fontWithName:@"HelveticaNeue" size:12]; 
    textField.attributedPlaceholder = 
    [[NSAttributedString alloc] initWithString:@"Enter fruit/vegetable name" 
                      attributes:@{ 
                          NSFontAttributeName : [UIFont fontWithName:@"HelveticaNeue" size:12.0] 
                         } 
    ]; 
1

Se si utilizza attributedPlaceholder, assicurarsi di impostare il tipo di carattere del textField essere lo stesso carattere utilizzato nel attributedString.

Problemi correlati