利用class_copyIvarList这个C函数,将所有的成员变量打印出来
这样我们就可以直接通过KVC进行属性设置了
- (void)awakeFromNib {
//修改占位文字颜色
[self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"]; //设置光标颜色和文字颜色一致
self.tintColor = self.textColor;
}
通过这个方法可以完成所有的效果,既具有通用性也简单
最后一个效果是
在获得焦点时改变占位文字颜色
在失去焦点时再改回去
//获得焦点时
- (BOOL)becomeFirstResponder {
//改变占位文字颜色
[self setValue:self.textColor forKeyPath:@"_placeholderLabel.textColor"]; return [super becomeFirstResponder];
}
//失去焦点时
- (BOOL)resignFirstResponder {
//改变占位文字颜色
[self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"]; return [super resignFirstResponder];
}
注:相关教程知识阅读请移步到IOS开发频道。











