一、經過屬性數組
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];
}