一、经过属性数组
a、 //文字属性(通常)框架
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];ide
attrs[NSForegroundColorAttributeName] = [UIColor blueColor];spa
NSAttributedString *placeholderStr = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attrs];继承
self.phoneTextField.attributedPlaceholder = placeholderStr;内存
b、稍微高级一点get
NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"手机号"];it
[placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor],io
NSFontAttributeName : [UIFont systemFontOfSize:20]table
} range:NSMakeRange(0, 1)];
[placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor]} range:NSMakeRange(1, 1)];
[placeholder setAttributes:@{NSForegroundColorAttributeName : [UIColor yellowColor]} range:NSMakeRange(2, 1)];
self.phoneTextField.attributedPlaceholder = placeholder;
2、经过重写UITextField的方法
继承UITextField的类,
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeholder drawInRect:CGRectMake(10, 10, 10, 1) withAttributes:@{
NSForegroundColorAttributeName :[UIColor blueColor],
NSFontAttributeName :[UIFont systemFontOfSize:10]
}];
}
使用的时候,若是是xib建立的textField,就吧xib中的textField的类名改为这个自定义的,若是是代码建立,就用这个自定义的textField去建立。
3、或者在UITextField中放个label,输入时隐藏,也能够达到效果
4、经过Runtime更改
运行时(Runtime):
* 苹果官方一套C语言库
* 能作不少底层操做(好比访问隐藏的一些成员变量\成员方法....)
#import "LHBTestTextField.h"
//要用运行时,必须导入该库
#import <objc/runtime.h>
static NSString * const LHBPlacerholderColorKeyPath = @"_placeholderLabel.textColor";
@implementation LHBTestTextField
//让代码建立的TextField的也能够用
- (instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
}
return self;
}
- (void)awakeFromNib
{
// 能够改
// UILabel *plcaeholderLabel = [self valueForKeyPath:@"_placeholderLabel"];
// plcaeholderLabel.textColor = [UIColor blueColor];
// 或者一句代码
// [self setValue:[UIColor yellowColor] forKeyPath:@"_placeholderLabel.textColor"];
// 设置光标颜色
// self.tintColor = [UIColor orangeColor];
// self.tintColor = self.textColor;//和文字颜色一致
//在RiderGirl中的应用
[self resignFirstResponder];
self.tintColor = [UIColor yellowColor];
}
#pragma mark - 当输入框聚焦时,调用,更改颜色。重写第一响应方法
- (BOOL)becomeFirstResponder
{
//成为第一响应,就修改placeholder的文字颜色
[self setValue:self.textColor forKeyPath:LHBPlacerholderColorKeyPath];
return [super becomeFirstResponder];
}
#pragma mark - 当焦点离开输入框时,调用,更改颜色。重写失去焦点方法
- (BOOL)resignFirstResponder
{
[self setValue:[UIColor grayColor] forKeyPath:LHBPlacerholderColorKeyPath];
return [super resignFirstResponder];
}
+ (void)initialize
{
// [self getIvars];
}
+ (void)getProperties
{
unsigned int count = 0;
//至关于拷贝出来,要手动管理内存
objc_property_t *propreties = class_copyPropertyList([UITextField class], &count);
for (NSInteger i=0; i<count; i++) {
//取出属性
objc_property_t property = propreties[i];
//打印属性名字
NSLog(@"%s --- %s",property_getName(property),property_getAttributes(property));
}
//释放内存
free(propreties);
}
+ (void)getIvars
{
unsigned int count = 0;
//至关于拷贝全部成员变量,要手动管理内存
Ivar *ivars = class_copyIvarList([UITextField class], &count);
for (NSInteger i=0; i<count; i++) {
//取出成员变量
// Ivar ivar = *(ivars + i);
Ivar ivar = ivars[i];//指向的是数组首元素时,能够当数组来用
//打印一下看看UITextField里面隐藏的成员变量的名字
NSLog(@"%s --- %s",ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}
//释放内存
free(ivars);
//会打印出一堆成员变量的名字,拿到这个成员变量的名字,能够经过kvc来更改内部属性,如今能够拿到_placeholderLabel
}
#pragma mark - 该方法只能实现高亮时更改placeholder的颜色。焦点离开时暂时改不了
//- (void)setHighlighted:(BOOL)highlighted
//{
// [self setValue:self.textColor forKeyPath:LHBPlacerholderColorKeyPath];
//}
#pragma mark - 写框架的话,能够在外部加一属性,重写set方法,方便外部更改
- (void)setPlaceholderColor:(UIColor *)placeholderColor
{
_placeholderColor = placeholderColor;
[self setValue:placeholderColor forKeyPath:LHBPlacerholderColorKeyPath];
}