iOS開發封裝篇-利用KVC修改UITextField的placeholderLabel顏色

大大小小也做了好幾個項目了,系統原生的控件越來越不能滿足開發(其實是產品經理)的需求了,接下來打算把開發中常用的控件封裝一下。剛好今天產品經理要求改一下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。




發佈了32 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章