iOS-改变UITextField的Placeholder颜色的三种方式
转自:http://blog.csdn.net/mazy_ma/article/details/51775670
有时,UITextField自带的Placeholder的颜色太浅或者不满足需求,所以需要修改,而UITextField没有直接的属性去修改Placeholder的颜色,所以只能通过其他间接方式去修改。
例如:系统默认的Placeholder颜色太浅
需要加深颜色,或者改变颜色
方法一:通过attributedPlaceholder属性修改Placeholder颜色
CGFloat viewWidth = self.view.bounds.size.width;
CGFloat textFieldX = 50;
CGFloat textFieldH = 30;
CGFloat padding = 30;
UITextField *textField = [[UITextField alloc] init];
textField.frame = CGRectMake(textFieldX, 100, viewWidth - 2 * textFieldX, textFieldH);
textField.borderStyle = UITextBorderStyleRoundedRect; // 边框类型
textField.font = [UIFont systemFontOfSize:14];
// 就下面这两行是重点
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请输入占位文字" attributes:
@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:textField.font
}];
textField.attributedPlaceholder = attrString;
[self.view addSubview:textField];
方法二:通过KVC修改Placeholder颜色
UITextField *textField1 = [[UITextField alloc] init];
textField1.frame = CGRectMake(textFieldX, CGRectGetMaxY(textField.frame) + padding, viewWidth - 2 * textFieldX, textFieldH);
textField1.borderStyle = UITextBorderStyleRoundedRect;
textField1.placeholder = @"请输入占位文字";
textField1.font = [UIFont systemFontOfSize:14];
// "通过KVC修改占位文字的颜色"
[textField1 setValue:[UIColor greenColor] forKeyPath:@"_placeholderLabel.textColor"];
[self.view addSubview:textField1];
方法三:通过重写UITextField的drawPlaceholderInRect:方法修改Placeholder颜色
1、自定义一个TextField继承自UITextField
2、重写drawPlaceholderInRect:方法
3、在drawPlaceholderInRect方法中设置placeholder的属性
// 重写此方法
-(void)drawPlaceholderInRect:(CGRect)rect {
// 计算占位文字的 Size
CGSize placeholderSize = [self.placeholder sizeWithAttributes:
@{NSFontAttributeName : self.font}];
[self.placeholder drawInRect:CGRectMake(0, (rect.size.height - placeholderSize.height)/2, rect.size.width, rect.size.height) withAttributes:
@{NSForegroundColorAttributeName : [UIColor blueColor],
NSFontAttributeName : self.font}];
}
总结:
1、当我们使用纯代码创建UITextField时,用第二种方法(KVC)修改占位文字颜色是最便捷的 。
2、当我们使用XIB或者Storyboard创建UITextField时,通过自定义UITextField,修改占位文字颜色是最适合的。
3、我们也可以在第三种重写方法中,通过结合第二种方法中的KVC修改属性来实现。
iOS-改变UITextField的Placeholder颜色的三种方式的更多相关文章
- Latex中如何设置字体颜色(三种方式)
1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/magenta/yellow}{text} 其中t ...
- 改变UITextField的Placeholder颜色
通过 attributedPlaceholder 属性来改变 if([textField respondsToSelector:@selector(setAttributedPlaceholder:) ...
- iOS 改变UITextField中光标颜色
第一种: [[UITextField appearance] setTintColor:[UIColor blackColor]]; 这个方法会影响整个app的所有UITextFiled... 第二种 ...
- placeholder 设置换行三种方式
在 html 中编写代码时保留代码换行 <textarea name="" id="" cols="30" rows="10 ...
- Latex中如何设置字体颜色(3种方式)
Latex中如何设置字体颜色(三种方式) 1.直接使用定义好的颜色 \usepackage{color} \textcolor{red/blue/green/black/white/cyan/ma ...
- iOS UITextField设置placeholder颜色
设置UITextField的placeholder颜色 UIColor *color = [UIColor blackColor]; textField.attributedPlaceholder = ...
- iOS字体加载三种方式
静态加载 动态加载 动态下载苹果提供的多种字体 其他 打印出当前所有可用的字体 检查某字体是否已经下载 这是一篇很简短的文章,介绍了 iOS 自定义字体加载的三种方式. 静态加载 这个可以说是最简单最 ...
- iOS --- UIWebView的加载本地数据的三种方式
UIWebView是IOS内置的浏览器,可以浏览网页,打开文档 html/htm pdf docx txt等格式的文件. safari浏览器就是通过UIWebView做的. 服务器将MIM ...
- iOS拨打电话的三种方式
iOS拨打电话的三种方式 1,这种方法,拨打完电话回不到原来的应用,会停留在通讯录里,而且是直接拨打,不弹出提示 1 2 var string = "tel:" + "1 ...
随机推荐
- SQL 语句 (二) --- SELECT
1 完整句法: SELECT [ ALL | DISTINCT TOP n [] WITH TIES select_list [INTO [new_table_name] ] [FROM {table ...
- Oracle SQL Developer 免费的DB2客户端
问题地址:https://stackoverflow.com/questions/8600735/is-there-any-opensource-db2-client 软件地址:http://www. ...
- MySQL之SQL语句的优化
仅供自己学习 结论写在前面: 1.尽量避免进行全表扫描,可以给where和order by涉及的列上建立索引 2.尽量在where子句中使用 !=或<>操作符,因为这样会导致引擎放弃索引而 ...
- 0323-DOM基础操作
dom操作 1.找到元素(标签对象) 标签名: document.getElementsByTagName()[]; //返回的是数组类型,需要有下标来找内容 属性: document.getElem ...
- [LeetCode] Number of Distinct Islands II 不同岛屿的个数之二
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- python数据挖掘_Json结构分析
json是一种轻量级的数据交换格式,也可以说是一种配置文件的格式 这种格式的文件是我们在数据处理经常会遇到的 python提供内置的模块json,只需要在使用前导入即可 你可以通过帮助函数查看json ...
- [Luogu 2265]路边的水沟
Description LYQ市有一个巨大的水沟网络,可以近似看成一个n*m的矩形网格,网格的每个格点都安装了闸门,我们将从水沟网络右下角的闸门到左上角的闸门的一条路径称为水流. 现给定水沟网的长和宽 ...
- HDU 2082 找单词
Problem Description 假 设有x1个字母A, x2个字母B,..... x26个字母Z,同时假设字母A的价值为1,字母B的价值为2,..... 字母Z的价值为26.那么,对于给定的字 ...
- [BZOJ]2589: Spoj 10707 Count on a tree II
Time Limit: 20 Sec Memory Limit: 400 MB Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor last ...
- BZOJ3810: [Coci2015]Stanovi
3810: [Coci2015]Stanovi Description Input 输入一行,三个整数,n, m, k Output 输出一个数,表示最小不满意度. Sample Input ...