iOS UITextField占位符字体大小/样式

时间:2011-07-20 20:01:00

标签: ios xcode uitextfield font-size

有人知道如何设置UITextField中占位符的字体大小吗?

感谢您的帮助

5 个答案:

答案 0 :(得分:5)

您可以通过以编程方式设置密钥@“_ placeholderLabel.font”的值来实现

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

答案 1 :(得分:4)

我发现可以通过在界面构建器中设置实际文本的字体样式来调整占位符字体的字体大小和样式。

答案 2 :(得分:1)

您可以覆盖drawPlaceholderInRect来执行此操作....

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

答案 3 :(得分:1)

实际上,还有另一种设置font \ font颜色属性的方式。

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,它解决了错误的垂直对齐问题。

答案 4 :(得分:0)

我遇到与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;
}
相关问题