iOS开发封装篇-利用KVC修改UITextField的placeholderLabel颜色
来源:互联网 发布:京东商城与淘宝的区别 编辑:程序博客网 时间:2024/05/16 15:11
大大小小也做了好几个项目了,系统原生的控件越来越不能满足开发(其实是产品经理)的需求了,接下来打算把开发中常用的控件封装一下。刚好今天产品经理要求改一下UITextField的占位文字的颜色,于是就打算搞一下。在思考怎么满足产品经理需求的时候,想到了三个方案,
一是利用富文本,可以很随意的修改placeholderLabel颜色和字体大小。具体代码如下:
NSMutableAttributedString *placehoder = [[NSMutableAttributedString alloc] initWithString:@"手机号"];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]} range:NSMakeRange(0, 1)];
[placehoder setAttributes:@{
NSForegroundColorAttributeName : [UIColor yellowColor],
NSFontAttributeName : [UIFont systemFontOfSize:30]
} range:NSMakeRange(1, 1)];
[placehoder setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor]} range:NSMakeRange(2, 1)];
self.phoneField.attributedPlaceholder = placehoder;
效果图如下:
二是重绘,- (void)drawPlaceholderInRect:(CGRect)rect方法里重新画图:这个就不上效果图了。
#import "WDDTextField.h"
@implementation WDDTextField
- (void)drawPlaceholderInRect:(CGRect)rect
{
[self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, 25) withAttributes:@{NSForegroundColorAttributeName : [UIColor grayColor],
NSFontAttributeName : self.font}];
}
@end
三是利用KVC修改placeholderLabel的颜色以及字体大小:
static NSString * const WDDPlaceholderColorkeyPath = @"_placeholderLabel.textColor";
static NSString * const WDDPlaceholderFontPath = @"_placeholderLabel.font";
@implementation WDDTextField
-(instancetype)initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
[self awakeFromNib];
}
return self;
}
- (void)awakeFromNib
{
// 设置光标颜色和文字颜色一致
self.tintColor = self.textColor;
// 不成为第一响应者
[self resignFirstResponder];
}
/**
* 当前文本框聚焦时就会调用
*/
- (BOOL)becomeFirstResponder
{
// 修改占位文字字体大小
if (self.placeholderHighlightedFont) {
[self setValue:self.placeholderHighlightedFont forKeyPath:WDDPlaceholderFontPath];
}else
{
[self setValue:self.font forKeyPath:WDDPlaceholderFontPath];
}
// 修改占位文字颜色
if (self.placeholderHighlightedColor) {
[self setValue:self.placeholderHighlightedColor forKeyPath:WDDPlaceholderColorkeyPath];
}else
{
[self setValue:self.textColor forKeyPath:WDDPlaceholderColorkeyPath];
}
return [super becomeFirstResponder];
}
/**
* 当前文本框失去焦点时就会调用
*/
- (BOOL)resignFirstResponder
{
// 修改占位文字字体大小
if (self.placeholderNormalFont) {
[self setValue:self.placeholderNormalFont forKeyPath:WDDPlaceholderFontPath];
}else
{
[self setValue:self.font forKeyPath:WDDPlaceholderFontPath];
}
// 修改占位文字颜色
if (self.placeholderNormalColor) {
[self setValue:self.placeholderNormalColor forKeyPath:WDDPlaceholderColorkeyPath];
}else
{
[self setValue:self.textColor forKeyPath:WDDPlaceholderColorkeyPath];
}
return [super resignFirstResponder];
}
效果图:
总结:最后变态的产品经理要求聚焦和失去焦点的时候占位文字的颜色和字体大小不一样,相比之下第三种方法简单粗暴,于是就用了第三种方案。更多的控件封装以后我会慢慢给出,欢迎访问我的github:https://github.com/Cehae。
使用方法:同时支持sb/xib。
- iOS开发封装篇-利用KVC修改UITextField的placeholderLabel颜色
- iOS利用storyboard修改UITextField的placeholder文字颜色
- iOS利用storyboard修改UITextField的placeholder文字颜色
- iOS利用storyboard修改UITextField的placeholder文字颜色
- iOS开发~修改UITextField的placeholder的字体大小、颜色
- 【iOS开发】修改UITextfield的Placeholder字体的颜色
- iOS开发修改UITextField的placeholder的颜色字体大小等
- (iOS开发)修改UITextField中placeholder的字体大小,颜色
- iOS - 修改UITextField的placeholder颜色
- iOS 之 修改UITextField的placeholder颜色
- [IOS]UITextField修改placeholder颜色
- 修改UItextField输入框placeholder的字体默认颜色和大小,采用KVC方式设置
- iOS开发-修改UITextField的placeholder字体颜色并垂直居中
- 利用StoryBoard修改placeholder颜色(UITextField的placeholder)
- [iOS]修改UITextfield的Placeholder字体的颜色
- iOS修改UITextField的placeholder的字体颜色大小
- iOS 修改UITextField 的placeholder字体大小和颜色
- IOS-OC 修改UITextField的Placeholder字体颜色
- libsvm使用教程
- [编写高质量iOS代码的52个有效方法](三)消息和运行期
- 关于文字环绕图片的问题
- 'xxx' declared `static' but never defined
- Bellovin(最长上升子序列)
- iOS开发封装篇-利用KVC修改UITextField的placeholderLabel颜色
- session和cookie
- 位移运算 特点
- MYSQL C API 入门教程
- PIE 二分加贪心
- 【C++】类 与继承
- win10 安装sqlserver 2008R2 出现问题:性能计数器注册表配置单元一致性
- Java压缩文件
- Linux C 第八课