标签:
1、通过属性
a、 //文字属性(一般)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor blueColor];
NSAttributedString *placeholderStr = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attrs];
self.phoneTextField.attributedPlaceholder = placeholderStr;
b、稍微高级一点
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"手机号"];
[placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],
NSFontAttributeName : [UIFont systemFontOfSize:20]
} range:NSMakeRange(0, 1)];
[placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} range:NSMakeRange(1, 1)];
[placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:NSMakeRange(2, 1)];
self.phoneTextField.attributedPlaceholder = placeholder;
二、通过重写UITextField的方法
继承UITextField的类,
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeholder drawInRect:CGRectMake(10, 10, 10, 1) withAttributes:@{
NSForegroundColorAttributeName :[UIColor blueColor],
NSFontAttributeName :[UIFont systemFontOfSize:10]
}];
}
使用的时候,如果是xib创建的textField,就吧xib中的textField的类名改成这个自定义的,如果是代码创建,就用这个自定义的textField去创建。
通过富文本改变UITextFieldPlaceholder颜色
标签:
原文地址:http://www.cnblogs.com/lhb-iOS/p/5573008.html