开发者

iOS UIText Field Place holder font size/style

开发者 https://www.devze.com 2023-03-22 06:07 出处:网络
Any开发者_Go百科body know of a way you can set the font size for a Placeholder in a UITextField?

Any开发者_Go百科body know of a way you can set the font size for a Placeholder in a UITextField?

Thanks for any help


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"];


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.


You can override drawPlaceholderInRect to do this....

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


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号