iOS-UITextField中给placeholder动态设置颜色的四种方法
2016-09-18 21:21
1336 查看
0.自定义
UITextField
1.设置占位文字的颜色找-->placeholderColor,结果发现
UITextField没有提供这个属性
2.在storyboard/xib中寻找设置placeholder的属性,也没有发现
3.发现
UITextField中有
- (void)drawPlaceholderInRect:(CGRect)rect;,这个方法用到QuartzD中的知识,可以试试;
4.打印
UITextField的子控件,查看是否有设置placeholder的属性
5.用运行时,查看UITextField中内部的结构,看有没有隐藏属性,利用KVC,可以强制使用任何私有属性
-
方法一:
缺点:只能设置一次状态,不能动态的改变Placeholder的颜色- (void)drawPlaceholderInRect:(CGRect)rect { // 设置富文本属性 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); } // 打印结果发现为空 // 猜测UITextField控件是懒加载的,调用这个方法的时候,它还没有加载storyboard/xib中的控件 // 利用GCD延时设置-->等storyboard/xib中的控件加载完毕在调用这个方法 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.01 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ DXLog(@"%@",self.subviews); }); // 打印结果是数组,里面有`UITextFieldLabel`,好像没有多大用
利用运行时,查看UITextField中内部的结构
// 这个方法调用时刻:当一个控件从xib或者storyBoard中创建之前,就会调用这个方法 - (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); } // 结果一大堆属性,其中有_placeholderLabel // 利用KVC设置它颜色,结果成功 UILabel *label = [self valueForKeyPath:@"_placeholderLabel"]; label.textColor = [UIColor orangeColor];
方法二:有了placeholderLabel.textColor
这个属性,在storyboard/xib中利用KVC设置placeholder颜色,如图
placeholder.png
方法三:用 setAttributedPlaceholder
方法
NSMutableDictionary *dict = [NSMutableDictionary dictionary]; dict[NSForegroundColorAttributeName] = [UIColor redColor]; NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict]; [self setAttributedPlaceholder:attribute];
下面用四种方法,动态的设置placeholderLabel
的颜色
方法一:addTarget
// 为了书写方便,便于管理,定义一个不可变的全局变量; 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]; }
方法二:代理-->遵守协议,自己做自己的代理
- (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]; }
方法四:重写文本框特有的方法
- (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]; }
文/船长_(简书作者)
原文链接:http://www.jianshu.com/p/e5c6c9b455dd
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- iOS-UITextField中给placeholder动态设置颜色的四种方法
- iOS-UITextField中给placeholder动态设置颜色
- ios开发——UITextField中设置placeholder字体颜色
- iOS-UITextView设置行间距,内容颜色(变相设置类似UITextField的placeholder)
- iOS开发UI高级 设置UITextField的placeholder颜色的几种方法
- iOS-改变UITextField(纯代码 /xib) 中的Placeholder颜色的几种方法
- ios 设置UITextField的placeholder大小颜色
- 【代码笔记】iOS-UITextField设置placeholder颜色
- [IOS]UITextField中设置placeholder字体颜色
- 【代码笔记】iOS-UITextField设置placeholder颜色
- iOS 设置UITextField的placeholder属性的颜色
- [IOS]UITextField修改placeholder颜色
- UITextField的placeholder文字的位置,颜色等的自定义设置
- android在代码中四种设置控件(以及TextView的文字颜色)背景颜色的方法
- iOS UITextField更改placeholder颜色
- 【iOS学习】TABBAR选择的TAB背景颜色设置方法
- IOS 开发更改UITextField的Placeholder颜色
- andorid,动态设置TextView文本中字体的颜色和格式,用Html.fromHtml方法
- iOS修改UITextField的提示语(placeholder)的字体大小和颜色
- ios navigationBar颜色比实际设置值浅的解决方法