iOS UIText Field Place holder字体大小/样式

有人知道你可以在UITextField中设置占位符的字体大小吗?

谢谢你的帮助

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

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

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

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

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

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

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

我遇到了与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; }