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

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

使用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"]; 

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

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

 [txtEmailAddress setValue:[UIColor blackColor] forKeyPath:@"_placeholderLabel.textColor"]; 

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

您可以重写drawPlaceholderInRect:(CGRect)rect ,以手动呈现占位符文本:

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

}

您将不得不inheritanceUITextField类并重写以下方法。

 - (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blackColor] setFill]; [[self placeholder] drawInRect:rect withFont:[UIFont italicSystemFontOfSize:17] lineBreakMode:UILineBreakModeCharacterWrap alignment:UITextAlignmentLeft]; } 

您可以使用此来更改占位符文本颜色。

 - (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor blueColor] setFill]; [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]]; } 

使用下面的代码。 这将帮助你。

  - (void) drawPlaceholderInRect:(CGRect)rect { [[UIColor redColor] setFill]; [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]]; }