/** 运行时 :runtime 可以访问隐藏的一些属性**在这个代码中 修改 文本框占位符 的颜色 */ + (void)initialize { [self getIvars]; [self getProperties]; } //获取所有属性 + (void)getProperties { unsigned int count = 0; objc_property_t *properties = class_copyPropertyList([UITextField class], &count); for (int i = 0; i < count; i++) { objc_property_t property = properties[i]; NSLog(@"%s <----> %s",property_getName(property), property_getAttributes(property)); } } //获取所有成员变量列表 + (void)getIvars { unsigned int count = 0; //拷贝出所有的成员变量列表 这是一个数组 可以访问隐藏的属性 Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i ++) { // Ivar ivar = *(ivars + i); Ivar ivar = ivars[i]; //等同上一句代码 NSLog(@"%s",ivar_getName(ivar)); } //释放内存 因为带有 copy free(ivars); } //视图加载出来时 - (void)awakeFromNib { // UILabel *placeHolderLabel = [self valueForKey:@"_placeholderLabel"]; // placeHolderLabel.textColor = [UIColor redColor]; //通过 kvc 赋值 // [self setValue:[UIColor orangeColor] forKeyPath:@"_placeholderLabel.textColor"]; //设置光标颜色和文字颜色一致 self.tintColor = self.textColor; [self resignFirstResponder]; } //文本框成为第一响应者 和放弃 时 设置文本框的 占位符的颜色 - (BOOL)becomeFirstResponder { [self setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; return [super becomeFirstResponder]; } - (BOOL)resignFirstResponder { [self setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; return [super resignFirstResponder]; } //外界可以通过访问这个属性 进行赋值 - (void)setPlaceHolderColor:(UIColor *)placeHolderColor { _placeHolderColor = placeHolderColor; [self setValue:placeHolderColor forKeyPath:@"_placeholderLabel.textColor"]; }
时间: 2024-10-24 18:35:07