第一种,直接方法。
// 设置光标颜色
self.tintColor = [UIColor whiteColor];
//设置占位符的位置
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
第二种重写 drawPlaceholderInRect 方法,可以指定占位符的相对位置。
-(void)drawPlaceholderInRect:(CGRect)rect{
CGPoint point = CGPointMake(10, (rect.size.height - self.font.lineHeight)*0.5);
[self.placeholder drawAtPoint:point withAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor],NSFontAttributeName:self.font}];
}
第三种用 KVC
//得到每个私有属性的方法
Ivar *ivarList = class_copyIvarList([UITextField class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivarList[i];
NSLog(@"%s", ivar_getName(ivar));
}
free(ivarList);
static NSString * const XMGPlaceholderColorKey = @"placeholderLabel.textColor"
// 设置默认的占位文字颜色
[self setValue:[UIColor blueColor] forKeyPath:XMGPlaceholderColorKey];