方法一:利用
富文本
// 创建一个富文本对象
NSMutableDictionary *attributes = [
NSMutableDictionary dictionary];
// 设置富文本对象的颜色
attributes[
NSForegroundColorAttributeName] = [
UIColor whiteColor];
// 设置UITextField的占位文字
self
.phoneTextField
.attributedPlaceholder = [[
NSAttributedString alloc] initWithString:
@"手机号" attributes:attributes];
方法二:利用
Runtime
获取私有的属性名称,利用
KVC
设置属性
设置占位符颜色
[_userName setValue:[UIColor whiteColor]
forKeyPath:@"_placeholderLabel.textColor"];
设置光标颜色
_userName.tintColor = [UIColor whiteColor];
扩充:系统还提供了很多类似的方法
- – textRectForBounds: //重写来重置文字区域
- – drawTextInRect: //改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了.
- – placeholderRectForBounds: //重写来重置占位符区域
- – drawPlaceholderInRect: //重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了
- – borderRectForBounds: //重写来重置边缘区域
- – editingRectForBounds: //重写来重置编辑区域
- – clearButtonRectForBounds: //重写来重置clearButton位置,改变size可能导致button的图片失真
- – leftViewRectForBounds:
- – rightViewRectForBounds: