我有一个自定义UITextField
,以便我得到一个自定义占位符文本颜色,正如答案所暗示的那样。但是,我也想在运行时更改占位符文本的颜色,所以我创建了一个属性。
// Overide the placholder text color
- (void) drawPlaceholderInRect:(CGRect)rect
{
[self.placeholderTextColor setFill];
[self.placeholder drawInRect:rect
withFont:self.font
lineBreakMode:UILineBreakModeTailTruncation
alignment:self.textAlignment];
}
- (void) setPlaceholderTextColor:(UIColor *)placeholderTextColor
{
// To verify this is being called and that the placeholder property is set
NSLog(@"placeholder text: %@", self.placeholder);
_placeholderTextColor = placeholderTextColor;
[self setNeedsDisplay]; // This does not trigger drawPlaceholderInRect
}
问题是文档说我不应该直接调用 drawPlaceholderInRect,并且[self setNeedsDisplay];
不起作用。有任何想法吗?