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。




0 0
原创粉丝点击