iOS UIText Field Place holder font size/style

拥有回忆 提交于 2019-12-08 14:14:13

问题


Anybody know of a way you can set the font size for a Placeholder in a UITextField?

Thanks for any help


回答1:


You can do it by programmatically setting up the value for the key @"_placeholderLabel.font"

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




回答2:


I found out the font size and style for the placeholder font can be adjusted by setting the font style for the actual text in interface builder.




回答3:


You can override drawPlaceholderInRect to do this....

- (void) drawPlaceholderInRect:(CGRect)rect {
         [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:13]];
}



回答4:


Actually, there is another form of setting font\font colour property.

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.
}

Pay attention to NSBaselineOffsetAttributeName, and it resolve the wrong vertical alignment problem.




回答5:


I faced same issue as Ankit , i solved it by changing font sizes in TextField delegates.

- (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;
}


来源:https://stackoverflow.com/questions/6767753/ios-uitext-field-place-holder-font-size-style

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!