- 思路分析:
- 0.自定義
UITextField
- 1.設(shè)置占位文字的顏色找-->placeholderColor,結(jié)果發(fā)現(xiàn)
UITextField
沒(méi)有提供這個(gè)屬性 - 2.在storyboard/xib中尋找設(shè)置placeholder的屬性,也沒(méi)有發(fā)現(xiàn)
- 3.發(fā)現(xiàn)
UITextField
中有- (void)drawPlaceholderInRect:(CGRect)rect;
,這個(gè)方法用到QuartzD中的知識(shí),可以試試; - 4.打印
UITextField
的子控件,查看是否有設(shè)置placeholder的屬性 - 5.用運(yùn)行時(shí),查看UITextField中內(nèi)部的結(jié)構(gòu),看有沒(méi)有隱藏屬性,利用KVC,可以強(qiáng)制使用任何私有屬性
- 0.自定義
方法一:
- 缺點(diǎn):只能設(shè)置一次狀態(tài),不能動(dòng)態(tài)的改變Placeholder的顏色
- (void)drawPlaceholderInRect:(CGRect)rect
{
// 設(shè)置富文本屬性
NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
dictM[NSFontAttributeName] = self.font;
dictM[NSForegroundColorAttributeName] = [UIColor redColor];
CGPoint point = CGPointMake(0, (rect.size.height - self.font.lineHeight) * 0.5);
[self.placeholder drawAtPoint:point withAttributes:dictM];
}
- (void)awakeFromNib
{
DXLog(@"%@",self.subviews);
}
// 打印結(jié)果發(fā)現(xiàn)為空
// 猜測(cè)UITextField控件是懶加載的,調(diào)用這個(gè)方法的時(shí)候,它還沒(méi)有加載storyboard/xib中的控件
// 利用GCD延時(shí)設(shè)置-->等storyboard/xib中的控件加載完畢在調(diào)用這個(gè)方法
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
DXLog(@"%@",self.subviews);
});
// 打印結(jié)果是數(shù)組,里面有`UITextFieldLabel`,好像沒(méi)有多大用
利用運(yùn)行時(shí),查看UITextField中內(nèi)部的結(jié)構(gòu)
// 這個(gè)方法調(diào)用時(shí)刻:當(dāng)一個(gè)控件從xib或者storyBoard中創(chuàng)建之前,就會(huì)調(diào)用這個(gè)方法
- (void)awakeFromNib
{
unsigned int count = 0;
Ivar *ivarList = class_copyIvarList([UITextField class], &count);
for (int i = 0; i<count; i++) {
Ivar ivar = ivarList[i];
DXLog(@"%s",ivar_getName(ivar));
}
free(ivarList);
}
// 結(jié)果一大堆屬性,其中有_placeholderLabel
// 利用KVC設(shè)置它顏色,結(jié)果成功
UILabel *label = [self valueForKeyPath:@"_placeholderLabel"];
label.textColor = [UIColor orangeColor];
方法二:有了placeholderLabel.textColor
這個(gè)屬性,在storyboard/xib中利用KVC設(shè)置placeholder顏色,如圖
placeholder.png
方法三:用 setAttributedPlaceholder
方法
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSForegroundColorAttributeName] = [UIColor redColor];
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
[self setAttributedPlaceholder:attribute];
下面用四種方法,動(dòng)態(tài)的設(shè)置placeholderLabel
的顏色
方法一:addTarget
// 為了書(shū)寫(xiě)方便,便于管理,定義一個(gè)不可變的全局變量;
static NSString * const DXPlaceholderColorKey = @"placeholderLabel.textColor";
- (void)awakeFromNib
{
[self addTarget:self action:@selector(editingDidBegin) forControlEvents:UIControlEventEditingDidBegin];
[self addTarget:self action:@selector(editingDidEnd) forControlEvents:UIControlEventEditingDidEnd];
}
- (void)editingDidBegin
{
[self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
}
- (void)editingDidEnd
{
[self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}
方法二:代理-->遵守協(xié)議,自己做自己的代理
- (void)awakeFromNib
{
self.delegate = self;
}
#pragma mark -------- <UITextFieldDelegate>---------
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}
方法三:通知
- (void)awakeFromNib
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidBegin) name:UITextFieldTextDidBeginEditingNotification object:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(editingDidEnd) name:UITextFieldTextDidEndEditingNotification object:self];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)editingDidBegin
{
[self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
}
- (void)editingDidEnd
{
[self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}
方法四:重寫(xiě)文本框特有的方法
- (void)awakeFromNib
{
[self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
}
- (BOOL)becomeFirstResponder
{
[self setValue:[UIColor whiteColor] forKeyPath:DXPlaceholderColorKey];
return [super becomeFirstResponder];
}
- (BOOL)resignFirstResponder
{
[self setValue:[UIColor grayColor] forKeyPath:DXPlaceholderColorKey];
return [super resignFirstResponder];
}