Nathan
Nathan

Reputation: 1649

iOS UIText Field Place holder font size/style

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

Thanks for any help

Upvotes: 0

Views: 10874

Answers (5)

iamhite
iamhite

Reputation: 429

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.

Upvotes: 1

Ashish Srivastava
Ashish Srivastava

Reputation: 101

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

Upvotes: 0

João Costa
João Costa

Reputation: 2355

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

Upvotes: 5

Nathan
Nathan

Reputation: 1649

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.

Upvotes: 4

Aemsn
Aemsn

Reputation: 348

You can override drawPlaceholderInRect to do this....

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

Upvotes: 1

Related Questions