第一種,直接方法。
// 設(shè)置光標(biāo)顏色
self.tintColor = [UIColor whiteColor];
//設(shè)置占位符的位置
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"
// 設(shè)置默認(rèn)的占位文字顏色
[self setValue:[UIColor blueColor] forKeyPath:XMGPlaceholderColorKey];