UITextField-修改占位文字和光标的颜色,大小
2016-11-14 12:03
309 查看
一.设置占位文字的颜色
方法一:利用富文本
/** 手机号输入框 */ @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设置属性
// 设置占位文字的颜色为红色(注意下面的'self'代表你要修改占位文字的UITextField控件) [self 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]]; }
二.设置光标颜色
// 设置光标的颜色 self.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:
文/YotrolZ(简书作者)
原文链接:http://www.jianshu.com/p/49b4eb97f41e
著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。
相关文章推荐
- UITextField-修改占位文字和光标的颜色,大小
- UITextField-修改占位文字和光标的颜色,大小
- iOS开发中设置UITextField的占位文字的颜色,和光标的颜色
- 修改UITextField的占位文字颜色的三种层次
- 设置UITextField占位文字的颜色
- UITextField限制输入长度,修改placeholder颜色和大小
- iOS改变UITextField占位文字颜色的三种方法
- UITextField(限制文字输入,调整光标,内容,占位文字显示的位置,以及基本使用)
- iOS修改UITextField的placeholder的字体颜色大小
- swift修改UITextfield的Placeholder字体大小和颜色
- iOS 原生导航栏 修改导航栏文字颜色 大小
- 设置 searchbar 的占位文字 以及 编辑文字的颜色和大小 背景颜色;
- 详解IOS 利用storyboard修改UITextField的placeholder文字颜色
- UITextField设置了占位文字内容之后,才能设置占位文字的颜色
- UITextField修改placeholder的大小和颜色
- android studio默认修改主题,字体,文字颜色,大小等属性的设置
- (iOS开发)修改UITextField中placeholder的字体大小,颜色
- iOS修改UITextField的提示语(placeholder)的字体大小和颜色
- 如何修改ios的导航条上面的文字颜色和大小等
- iOS利用storyboard修改UITextField的placeholder文字颜色