我有UITextField
我需要更改Placeholder
字体和颜色,我在drawRect方法中调用以下方法,
-(void) setFontColorForPlaceHolder
{
for(id obj in [[self baseScrollView] subviews])
{
if([obj isKindOfClass:[UITextField class]])
{
[obj setAttributedPlaceholder:[[NSAttributedString alloc]initWithString:[obj placeholder] attributes:@{
NSFontAttributeName:kFutura_Medium_14 ,NSForegroundColorAttributeName:[UIColor redColor]
}]];
}
}
}
此处COLOR正在改变,但FONT未设置。这里有什么问题setAttributedPlaceholder
。
答案 0 :(得分:6)
我在iOS 7.1中进行了测试,发现NSFontAttributeName
没有对UITextField
的占位符文字做任何事情。
但是,当我更改UITextField本身的字体时,占位符文本字体也改变了:
aUITextField.font = [UIFont fontWithName:@"HelveticaNeue-Thin" size:20.0f];
答案 1 :(得分:2)
[textField setValue:your_color forKeyPath:@"_placeholderLabel.textColor"];
要设置占位符的字体,只需在编写textholder之前为textfield设置字体。
textField.font = your font here;
[textField setValue:your_color forKeyPath:@"_placeholderLabel.textColor"];
textField.placeholder = "your placeholder string here...";
答案 2 :(得分:0)
SWIFT 3 在您的自定义类中覆盖此:
override func placeholderRect(forBounds bounds: CGRect) -> CGRect {
//set initial attributed placeholder color and font
self.attributedPlaceholder = NSAttributedString(string:self.placeholder != nil ? self.placeholder! : "", attributes:[NSForegroundColorAttributeName: placeholderColor, NSFontAttributeName: placeholderTextFont])
return bounds
}