第一种方法实现:
1、自定义ZZPlaceholderColorTextField集成于UITextField
2、
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor whiteColor];
NSMutableDictionary *attrs = [NSMutableDictionary dictionaryWithCapacity:1];
attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:attrs];
// Drawing code
}
第二种方法实现:
2、
- (void)drawRect:(CGRect)rect {
self.tintColor = [UIColor whiteColor];
// Drawing code
}
#pragma mark 重写drawPlaceholderInRect方法
- (void)drawPlaceholderInRect:(CGRect)rect {
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
attrs[NSFontAttributeName] = self.font;
/*
//画出占位符
CGRect placeholderRect;
placeholderRect.size.width = rect.size.width;
placeholderRect.size.height = rect.size.height;
placeholderRect.origin.x = 0;
placeholderRect.origin.y = (rect.size.height - self.font.lineHeight) * 0.5;
[self.placeholder drawInRect:placeholderRect withAttributes:attrs];
*/
//或者
CGPoint placeholderPoint = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5);
[self.placeholder drawAtPoint:placeholderPoint withAttributes:attrs];
}