ios textfield 修改 placeholder color 颜色
2015-10-21 10:01
661 查看
UITextField修改placeholder color有很多种方式,但选择哪一种看你自己,有一些方法并不建议,同时希望大家分享没有摘录的方法,tks
方法一(不推荐):
[_textPhone setValue:[UIColor whiteColor]
forKeyPath:@"_placeholderLabel.textColor"];
最直接一句话搞定,使用KVC模式直接设置TestField的私有属性的值。没错,_placeholderLabel是一个私有的属性,使用这种方式虽然简单但并不是很好的做法,如果Apple修改(虽然至今没有修改)了该属性名后,会在之后的运行中遭遇崩溃,而编译器却无法识别。
有人说使用KVC修改私有属性可能提交 app store审核不通过,但也有人说能通过,自己试试吧~
方法二(不推荐):
意思同方法一
方法三:
UIColor *color = [UIColor whiteColor];
_textPhone.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_textPhone.placeholder attributes:@{NSForegroundColorAttributeName: color}];使用UITextField的共有属性attributedPlaceholder,妥妥的,虽然代码略长~
方法四:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor *color = [UIColor whiteColor];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}];
// 以下注释的方法,在ios7后被放弃
// [[UIColor whiteColor] setFill];
// [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
//
}
创建一个类继承UITextField,并重写该方法,使用该类即可,略麻烦,但如果是公用textField,推荐这种方法,可以添加更多设置。
方法一(不推荐):
[_textPhone setValue:[UIColor whiteColor]
forKeyPath:@"_placeholderLabel.textColor"];
最直接一句话搞定,使用KVC模式直接设置TestField的私有属性的值。没错,_placeholderLabel是一个私有的属性,使用这种方式虽然简单但并不是很好的做法,如果Apple修改(虽然至今没有修改)了该属性名后,会在之后的运行中遭遇崩溃,而编译器却无法识别。
有人说使用KVC修改私有属性可能提交 app store审核不通过,但也有人说能通过,自己试试吧~
方法二(不推荐):
意思同方法一
方法三:
UIColor *color = [UIColor whiteColor];
_textPhone.attributedPlaceholder = [[NSAttributedString alloc] initWithString:_textPhone.placeholder attributes:@{NSForegroundColorAttributeName: color}];使用UITextField的共有属性attributedPlaceholder,妥妥的,虽然代码略长~
方法四:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
UIColor *color = [UIColor whiteColor];
self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:@{NSForegroundColorAttributeName: color}];
// 以下注释的方法,在ios7后被放弃
// [[UIColor whiteColor] setFill];
// [[self placeholder] drawInRect:rect withFont:[UIFont systemFontOfSize:16]];
//
}
创建一个类继承UITextField,并重写该方法,使用该类即可,略麻烦,但如果是公用textField,推荐这种方法,可以添加更多设置。
相关文章推荐
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 峰回路转,Firefox 浏览器即将重返 iOS 平台
- 不可修补的 iOS 漏洞可能导致 iPhone 4s 到 iPhone X 永久越狱
- iOS 12.4 系统遭黑客破解,漏洞危及数百万用户
- 每日安全资讯:NSO,一家专业入侵 iPhone 的神秘公司
- [转][源代码]Comex公布JailbreakMe 3.0源代码
- js兼容的placeholder属性详解
- 实现placeholder效果的方案汇总
- jQuery实现的一个自定义Placeholder属性插件
- 两种方法基于jQuery实现IE浏览器兼容placeholder效果
- Color Cop V5.4.3_适合网页设计师/多用途的颜色采取器_汉化绿色版 下载
- 基于jQuery的让非HTML5浏览器支持placeholder属性的代码
- 讲解iOS开发中基本的定位功能实现
- js判断客户端是iOS还是Android等移动终端的方法
- IOS开发环境windows化攻略
- CSS的color颜色使用说明
- 检测iOS设备是否越狱的方法
- .net平台推送ios消息的实现方法
- 探讨Android与iOS,我们将何去何从?