xcode iOS UIText 字段占位符字体大小/样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6767753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS UIText Field Place holder font size/style
提问by Nathan
Anybody know of a way you can set the font size for a Placeholder in a UITextField?
有人知道可以在 UITextField 中为占位符设置字体大小的方法吗?
Thanks for any help
谢谢你的帮助
回答by Jo?o Costa
You can do it by programmatically setting up the value for the key @"_placeholderLabel.font"
您可以通过以编程方式设置键 @"_placeholderLabel.font" 的值来实现
[self.input setValue:[UIFont fontWithName: @"American Typewriter Bold" size: 20] forKeyPath:@"_placeholderLabel.font"];
[self.input setValue:[UIFont fontWithName: @"American Typewriter Bold" size: 20] forKeyPath:@"_placeholderLabel.font"];
回答by Nathan
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.
我发现可以通过在界面构建器中设置实际文本的字体样式来调整占位符字体的字体大小和样式。
回答by iamhite
Actually, there is another form of setting font\font colour property.
实际上,还有另一种设置 font\font color 属性的形式。
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.
注意NSBaselineOffsetAttributeName
,它解决了错误的垂直对齐问题。
回答by Aemsn
You can override drawPlaceholderInRect to do this....
您可以覆盖 drawPlaceholderInRect 来执行此操作....
- (void) drawPlaceholderInRect:(CGRect)rect {
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:13]];
}
回答by Ashish Srivastava
I faced same issue as Ankit , i solved it by changing font sizes in TextField delegates.
我遇到了与 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;
}