UITextField 关于placeholder属性的用法
2016-07-14 17:35
483 查看
关于UITextField设置占位文字,最常用的就是直接赋值,如下:
但是,在我们的开发中,会遇到各种各样的需求,这就需要我们了解placeholder这个属性更多的作用,下面我列举了一些:例如修改占位文字和光标的颜色,大小等…
方法二 : 利用Runtime获取私有的属性名称,利用KVC设置属性
注意 : _placeholderLabel.textColor是不可乱写的哦,我们是怎么获取到这个属性的呢?请看下文:
查看打印,找出可能的属性名称,试试便知~
完整代码 : 自定义的UITextField,获取到焦点(编辑状态)的时候是白色,失去焦点(非编辑状态)的时候是灰色:
方法三 : 将占位文字画上去(重写- (void)drawPlaceholderInRect:(CGRect)rect;)
可以用来设置光标与占位的间距
扩充 : 系统还提供了很多类似的方法:
textField.placeholder = @"请输入用户名";
但是,在我们的开发中,会遇到各种各样的需求,这就需要我们了解placeholder这个属性更多的作用,下面我列举了一些:例如修改占位文字和光标的颜色,大小等…
一、设置占位文字的颜色
方法一 : 利用富文本/** 手机号输入框 */ @property (weak, nonatomic) IBOutlet UITextField *phoneTextField; - (void)viewDidLoad { [super viewDidLoad]; // 创建一个富文本对象 NSMutableDictionary *attributes = [NSMutableDictionary dictionary]; // 设置富文本对象的颜色 attributes[NSForegroundColorAttributeName] = [UIColor whiteColor]; // 设置UITextField的占位文字 self.phoneTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"手机号" attributes:attributes]; }
方法二 : 利用Runtime获取私有的属性名称,利用KVC设置属性
// 设置占位文字的颜色为红色 [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
注意 : _placeholderLabel.textColor是不可乱写的哦,我们是怎么获取到这个属性的呢?请看下文:
// 只调用一次(自定义UITextField) + (void)initialize { [self getIvars]; } // 获取私有变量名称 + (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar)); } }
查看打印,找出可能的属性名称,试试便知~
完整代码 : 自定义的UITextField,获取到焦点(编辑状态)的时候是白色,失去焦点(非编辑状态)的时候是灰色:
#import "YCTextField.h" #import <objc/runtime.h> #define YCplaceholderTextColor @"_placeholderLabel.textColor" @implementation YCTextField + (void)initialize { [self getIvars]; } // 获取私有变量名称 + (void)getIvars { unsigned int count = 0; Ivar *ivars = class_copyIvarList([UITextField class], &count); for (int i = 0; i < count; i++) { Ivar ivar = ivars[i]; NSLog(@"%s----%s", ivar_getName(ivar), ivar_getTypeEncoding(ivar)); } } - (void)awakeFromNib { // 设置光标的颜色 self.tintColor = self.textColor; } // 获取到焦点 - (BOOL)becomeFirstResponder { // 利用运行时获取key,设置占位文字的颜色 [self setValue:self.textColor forKeyPath:YCplaceholderTextColor]; return [super becomeFirstResponder]; } // 失去焦点 - (BOOL)resignFirstResponder { // 利用运行时获取key,设置占位文字的颜色 [self setValue:[UIColor grayColor] forKeyPath:YCplaceholderTextColor]; return [super resignFirstResponder]; } @end
方法三 : 将占位文字画上去(重写- (void)drawPlaceholderInRect:(CGRect)rect;)
- (void)drawPlaceholderInRect:(CGRect)rect { [[UIColor orangeColor] set]; [self.placeholder drawInRect:rect withFont:[UIFont systemFontOfSize:20]]; }
二、设置光标颜色
// 设置光标的颜色 textField.tintColor = [UIColor redColor];
三、设置占位文字的偏移
重写-(CGRect)placeholderRectForBounds:(CGRect)bounds;方法可以用来设置光标与占位的间距
// 控制placeHolder的位置,左右缩20 -(CGRect)placeholderRectForBounds:(CGRect)bounds { //return CGRectInset(bounds, 20, 0); CGRect inset = CGRectMake(bounds.origin.x+50, bounds.origin.y, bounds.size.width -10, bounds.size.height);//更好理解些 return inset; }
扩充 : 系统还提供了很多类似的方法:
– textRectForBounds: // 重写来重置文字区域 – drawTextInRect: // 改变绘文字属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了. – placeholderRectForBounds: // 重写来重置占位符区域 – drawPlaceholderInRect: // 重写改变绘制占位符属性.重写时调用super可以按默认图形属性绘制,若自己完全重写绘制函数,就不用调用super了 – borderRectForBounds: // 重写来重置边缘区域 – editingRectForBounds: //重写来重置编辑区域 – clearButtonRectForBounds: //重写来重置clearButton位置,改变size可能导致button的图片失真 – leftViewRectForBounds: – rightViewRectForBounds:
相关文章推荐
- 使用UITextField限制输入金额是正确小数
- iOS开发之自定义UITextField的方法
- iOS App开发中UITextField组件的常用属性小结
- iOS应用开发中使UITextField实现placeholder属性的方法
- 解决iOS7上UITextField限制字数输入导致崩溃问题的方法
- IOS UI学习教程之设置UITextField各种属性
- Swift社交应用文本输入优化汇总
- UITextField常用delegate
- UITextField
- iOS开发1:UITextField控件属性
- iOS UITextField 拾遗(下)
- [Swift]UIKit学习之UITextField的用法
- IOS开发Swift语言UITextField设置靠左的间距
- IOS开发Swift语言键盘遮挡输入框(UITextField)
- iOS UITextField相关
- UIKit,UIView
- iOS关于系统键盘的长度限制
- cocos2d-x 一些有用的函数
- UITextField的使用
- UITextField输入框的字靠右一点显示