Change placeholder text color
I'd like to change the color of the placeholder text I set in my UITextField
controls, to make it black.
I'd prefer to do this without using normal text as the placeholder and having to override all the methods to imitate the behaviour of a placeholder.
I believe if I override this method:
- (void)drawPlaceholderInRect:(CGRect)rect
then I should be able to do this. But I'm unsure how to access the actual placeholder object from within this method.
由于在iOS 6的UIViews中引入了属性字符串,因此可以为占位符文本指定一个颜色,如下所示:
if ([textField respondsToSelector:@selector(setAttributedPlaceholder:)]) {
UIColor *color = [UIColor blackColor];
textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholderText attributes:@{NSForegroundColorAttributeName: color}];
} 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.
}
Easy and pain-free, could be an easy alternative for some.
_placeholderLabel.textColor
Not suggested for production, Apple may reject your submission.
您可以重写drawPlaceholderInRect:(CGRect)rect以手动呈现占位符文本:
- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
}
链接地址: http://www.djcxy.com/p/73496.html
上一篇: 如何浏览文本框(下一个/完成按钮)
下一篇: 更改占位符文字颜色