修改UITextFiled的placeholder颜色的几种方法
2、重写方法
3、运行时(Runtime)
· 苹果官方一套C语言库
· 能做很多底层操作
注意:修改placeholder还有很多种方法,这里只列举了比较常用的,并不全面
1、使用属性
<span style="font-size:14px;color:#333333;">@property(nonatomic,strong)UITextField *nameField;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
// NSAttributedString:富文本设置
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"我是富文本" attributes:attrs];
self.nameField.attributedPlaceholder = placeholder;
NSMutableAttributedString *mutableAttrs = [[NSMutableAttributedString alloc] initWithString:@"加加加"];
[mutableAttrs setAttributedString:@{NSForegroundColorAttributeName:[UIColor grayColor]}];
self.nameField.attributedPlaceholder = mutableAttrs;
</span>
2、重写方法
<span style="font-size:14px;color:#333333;">-(void)awakeFromNib{
// 改变光标颜色
self.tintColor = [UIColor whiteColor];
}
-(void)drawPlaceholderInRect:(CGRect)rect{
[self.placeholder drawInRect:CGRectMake(5, 15, self.width, self.height) withAttributes:@{
NSForegroundColorAttributeName:[UIColor grayColor],
NSFontAttributeName:self.font
}];
}</span>
3、运行时(Runtime)
· 苹果官方一套C语言库
· 能做很多底层操作
<span style="font-size:14px;color:#333333;">unsigned int count = 0;
Ivar *ivars = class_copyIvarList([UITextField class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = *(ivars + i);
// 打印变量
NSLog(@"%s",ivar_getName(ivar));
}
// 释放
free(ivars);
</span>
注意:修改placeholder还有很多种方法,这里只列举了比较常用的,并不全面