如何更改占位符文本颜色 - iOS

8 iphone ios

如何更改我在UITextField控件中设置的占位符文本的颜色,使其变黑?

Vin*_*odh 45

使用KVC

[yourtextfield setValue:[UIColor colorWithRed:120.0/255.0 green:116.0/255.0 blue:115.0/255.0 alpha:1.0] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

您可以将自己的颜色设置为占位符


cha*_*dan 13

这是通过KVO更改占位符颜色的最佳方法...

[txtEmailAddress setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"];
Run Code Online (Sandbox Code Playgroud)

我希望它可以帮助你改变占位符的颜色.谢谢


小智 7

您可以这样覆盖drawPlaceholderInRect:(CGRect)rect以手动呈现占位符文本:

- (void) drawPlaceholderInRect:(CGRect)rect {
[[UIColor blueColor] setFill];
[[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
Run Code Online (Sandbox Code Playgroud)

}