iOS UIText Field Place размер шрифта / стиль - PullRequest
0 голосов
/ 21 июля 2011

Кто-нибудь знает, как можно установить размер шрифта для заполнителя в UITextField?

Спасибо за любую помощь

Ответы [ 5 ]

5 голосов
/ 26 февраля 2013

Вы можете сделать это, программно установив значение для ключа @ "_ placeholderLabel.font"

[self.input setValue:[UIFont fontWithName: @"American Typewriter Bold" size: 20] forKeyPath:@"_placeholderLabel.font"];

4 голосов
/ 27 июля 2011

Я обнаружил, что размер и стиль шрифта для шрифта-заполнителя можно настроить, установив стиль шрифта для фактического текста в конструкторе интерфейса.

1 голос
/ 23 марта 2015

На самом деле существует другая форма установки свойства font \ font color.

if ([self.textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
    self.textField.attributedPlaceholder = [[NSAttributedString alloc]
                                       initWithString:placeholder
                                       attributes:@{
                                                    NSForegroundColorAttributeName: newColor,
                                                    NSFontAttributeName:font,
                                                    NSBaselineOffsetAttributeName:[NSNumber numberWithFloat:offset]
                                                    }];
} else {
    NSLog(@"Cannot set placeholder text's color, because deployment target is earlier than iOS 6.0");
        // TODO: Add fall-back code to set placeholder color.
}

Обратите внимание на NSBaselineOffsetAttributeName, и это решит проблему неправильного вертикального выравнивания.

1 голос
/ 21 июля 2011

Вы можете переопределить drawPlaceholderInRect, чтобы сделать это ....

- (void) drawPlaceholderInRect:(CGRect)rect {
         [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:13]];
}
0 голосов
/ 09 декабря 2014

Я столкнулся с той же проблемой, что и Ankit, я решил ее, изменив размеры шрифта в делегатах TextField.

- (BOOL)textFieldShouldClear:(UITextField *)textField{
    [textField setFont:[UIFont fontWithName: @"American Typewriter Bold" size: 12]];
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (range.location == 0 && range.length == 0) {
        [textField setFont:[UIFont fontWithName: @"American Typewriter Bold" size: 18]];
    }
    if(range.location == 0 && range.length == 1){
        [textField setFont:[UIFont fontWithName: @"American Typewriter Bold" size: 12]];
    }
    return YES;
}
...