[BS-19]更改UITextField的placeholder文字颜色的5种方法
更改UITextField的placeholder文字颜色的5种方法
想要达到的目标是:一个页面上有多个UITextField,当用户聚焦某textField时,该文本框的placeholder的文字会灰色变为白色,当文本框失去焦点时,placeholder颜色从白色再变回灰色。
1.放置UILabel
最简单最笨的方法是在每个textField里放一个UILabel来充当placeholder,当该textField聚焦时,让placeholder的文字会灰色变为白色,失焦后从白色再变回灰色。这种方法需要对每个UILabel和TextField拖线,通过监听键盘通知或者UITextField的代理来获悉textField是否聚焦,然后写一堆if语句来判断。
2.修改textField.attributedPlaceholder属性
//通过NSAttributedString来更改placeholder颜色。此种方法比较麻烦的是,需要知道页面上所有的textField什么时候聚焦,什么时候失焦(有两种方法:A.监听键盘弹出的通知 B.通过UITextField的代理方法textFieldDidBeginEditing),然后再判断哪个是白色,哪个是灰色。如果页面上textField非常多,就需要写很多的if语句。
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc]initWithString:@"手机号" attributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}];
self.phoneTextField.attributedPlaceholder = attrString;
3. 利用UITextField的子类重写drawPlaceholderInRect:方法
具体实现见代码:
//该方法的好处是不用获取Xib中textField的引用,不用拖线,直接将Xib中textField的class改为自定义类即可。以后有任何textField想要改变placeholder的颜色,直接定义为该类即可。但还是需要自己监控UITextField是否聚焦。 #import "WZCustomPlaceholderTextField.h" @implementation WZCustomPlaceholderTextField
/**
@interface NSString(NSStringDrawing) NSString的分类
- (CGSize)sizeWithAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
- (void)drawAtPoint:(CGPoint)point withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
- (void)drawInRect:(CGRect)rect withAttributes:(nullable NSDictionary<NSString *, id> *)attrs NS_AVAILABLE(10_0, 7_0);
@end
*/
- (void)drawPlaceholderInRect:(CGRect)rect {//rect代表该自定义textField的frame/rect
//因为placeholder属于NSString类型,所有的NSString都有drawInRect方法,但此方法似乎只在draw开头方法中有效
[self.placeholder drawInRect:CGRectMake(, , rect.size.width, rect.size.height) withAttributes:@{
NSForegroundColorAttributeName:[UIColor whiteColor],
NSFontAttributeName:[UIFont systemFontOfSize:]
}]; } - (void)drawRect:(CGRect)rect {//rect代表该自定义textField的frame/rect
[super drawRect:rect]; //调用父类UITextField的drawRect方法,将自定义尺寸传进去。必须调用父类 } @end
4.利用UITextField的子类重写drawRect:方法,在drawRect中通过[self.placeholder drawInRect:]来进行设置。
5.通过KVC向UITextField隐藏的内部实例变量_placeholderLabel设值。
利用runtime打印出UITextField所有的实例变量,发现有个叫_placeholderLabel的内部实例变量,在自定义类中通过KVC设值。此方法好处是:利用KVC设值方便简洁,而且可以写在任何位置。本例中我们重写becomeFirstResponder:和resignFirstResponder方法来监控UITextField是否聚焦,然后在其中利用KVC设值。
// WZTextField.m // 利用runtime打印出UITextField所有的实例变量,发现有个叫_placeholderLabel的内部实例变量,在自定义类中通过KVC设值。
// #import "WZTextField.h" @implementation WZTextField
//调用顺序1 (从xib创建时调用)
- (instancetype)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
}
return self;
}
//调用顺序2 (从xib创建时调用)
- (void)awakeFromNib { } //调用顺序3 (无论xib还是纯代码创建都会调用)
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
self.tintColor = self.textColor; //设置光标颜色和文字颜色一样
} - (BOOL)becomeFirstResponder {
[self setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
return [super becomeFirstResponder];
} - (BOOL)resignFirstResponder {
[self setValue:[UIColor grayColor] forKeyPath:@"_placeholderLabel.textColor"];
return [super resignFirstResponder];
} @end
[BS-19]更改UITextField的placeholder文字颜色的5种方法的更多相关文章
- 修改UITextField的占位文字颜色的三种层次
层次一:利用富文本 // 描述占位文字属性 NSMutableDictionary *dict = [NSMutableDictionary dictionary] ; dict[NSForegrou ...
- iOS 更改状态栏、导航栏颜色的几种方法
ios上状态栏 就是指的最上面的20像素高的部分状态栏分前后两部分,要分清这两个概念,后面会用到: 前景部分:就是指的显示电池.时间等部分:背景部分:就是显示黑色或者图片的背景部分: (一)设置sta ...
- input placeholder 文字颜色修改
placeholder 文字颜色修改 input::-webkit-input-placeholder{ color:red; } input::-moz-placeholder{ /* Mozill ...
- IE下实现类似CSS3 text-shadow文字阴影的几种方法
IE下实现类似CSS3 text-shadow文字阴影的几种方法 一.开始的擦边话 为了测试IE9浏览器,下午晃晃荡荡把系统换成window7的了.果然,正如网上所传言的一样,IE9浏览器确实不支持C ...
- JS实现随机颜色的3种方法与颜色格式的转化
JS实现随机颜色的3种方法与颜色格式的转化 随机颜色和颜色格式是我们在开发中经常要用到的一个小功能,网上相关的资料也很多,想着有必要总结一下自己的经验.所以这篇文章主要介绍了JS实现随机颜色的3种 ...
- Android(java)学习笔记106:Android设置文本颜色的4种方法
1. Android设置文本颜色的4种方法: (1)利用系统自带的颜色类: tv.setTextColor(android.graphics.Color.RED); (2)数字颜色表示: tv.set ...
- 如何更改UITextField 的placeholder 的字体颜色
storyboard 中这样设置 具体步骤: 1.在User Defined Runtime Attributes中添加一个Key. 2.输入Key Path(这里我们输入_placeholderLa ...
- UITextField的placeholder文字的位置,颜色等的自定义设置
//控制placeHolder的位置,左右缩20 -(CGRect)placeholderRectForBounds:(CGRect)bounds { CGRect inset = CGRectMak ...
- placeholder文字颜色与是否显示兼容性
1.ie显示问题 <script type="text/javascript"> $(document).ready(function(){ var doc=docum ...
随机推荐
- Redis Cluster 3.0搭建与使用
Redis Cluster终于出了Stable,这让人很是激动,等Stable很久了,所以还是先玩玩. 一. 集群简单概念. Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施( ...
- Laptop Issue Letter (读取Excel中指定内容,然后生成新的Excel文件)
$xl = New-Object -ComObject "Excel.Application" $cmdbwb = $xl.Workbooks.Open("F:\Ivan ...
- 【转】Java跨平台原理
原文地址:http://www.cnblogs.com/gw811/archive/2012/09/09/2677386.html 1.是么是平台 Java是可以跨平台的编程语言,那我们首先得知道什么 ...
- 2016.07.04,英语,《Vocabulary Builder》Unit 23
text comes from a Latin verb that means 'to weave'. textile: ['tekstaɪl] adj. 纺织的 n. 纺织品; texture: [ ...
- jQuery+CSS 简单代码实现遮罩层( 兼容主流浏览器 )
/* ** jQuery版本:jQuery-1.8.3.min.js ** 浏览器:Chrome( v31.0.1650.63 m ),IE11,Firefox( v32.0.1 ),IETester ...
- UItableview里面的header、footer
#import "ViewController.h" #import "MJRefresh.h" @interface ViewController () { ...
- jQuery 中 on 方法-----给未来元素添加事件
<li class='clear dir-li'> <div class='left title'> 添加到目录:</div> <div class='lef ...
- 低功耗蓝牙4.0BLE编程-nrf51822开发(9)
Android 4.3以后的系统自动支持蓝牙4.0规范的低功耗蓝牙(BLE).在android4.3之前,蓝牙4.0支持是由手机厂家加入支持的,接口各异,导致开发一个支持蓝牙4.0程序支持市面上的手机 ...
- sqlserver 获取当前操作的数据库名称
Select Name From Master..SysDataBases Where DbId=(Select Dbid From Master..SysProcesses Where Spid = ...
- MySQL- 锁(2)
InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点意味着:只有 ...