iOS_黑(5)_UITextField修改全局placeholder顏色

一丶原理

修改placeholder的方法:

1.
myTextField.placeholder = @"請(qǐng)輸入用戶名!";  
[myTextField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];  
[myTextField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];

2.建議使用:
NSMutableAttributedString *placeholderAtbString = [[NSMutableAttributedString alloc] initWithString:placeholder];
[placeholderAtbString addAttribute:NSForegroundColorAttributeName
                    value:kMianColor_8997B0
                    range:NSMakeRange(0, placeholder.length)];
self.attributedPlaceholder = placeholderAtbString;

修改全局:

runtime替換系統(tǒng)placeholder方法;

二丶代碼

#import "UITextField+HYBPlaceHolder.h"

@implementation UITextField (HYBPlaceHolder)

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class selfClass = [self class];
        
        SEL oriSEL = @selector(setPlaceholder:);
        Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
        
        SEL cusSEL = @selector(ZB_setPlaceholder:);
        Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);
        
        BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
        if (addSucc) {
            class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
        }else {
            method_exchangeImplementations(oriMethod, cusMethod);
        }
        
    });
    
}

- (void)ZB_setPlaceholder:(NSString *)placeholder{
    
    NSMutableAttributedString *placeholderAtbString = [[NSMutableAttributedString alloc] initWithString:placeholder];
    [placeholderAtbString addAttribute:NSForegroundColorAttributeName
                        value:kMianColor_8997B0
                        range:NSMakeRange(0, placeholder.length)];
    self.attributedPlaceholder = placeholderAtbString;
    
    [self ZB_setPlaceholder:placeholder];
}


@end
注意事項(xiàng):

項(xiàng)目中的Placeholder必須用代碼去設(shè)置,xib的并不走setPlaceholder方法;
xib實(shí)現(xiàn)代碼:

+ (void)load{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        Class selfClass = [self class];
        {
            SEL oriSEL = @selector(initWithCoder:);
            Method oriMethod = class_getInstanceMethod(selfClass, oriSEL);
            
            SEL cusSEL = @selector(ZB_initWithCoder:);
            Method cusMethod = class_getInstanceMethod(selfClass, cusSEL);
            
            BOOL addSucc = class_addMethod(selfClass, oriSEL, method_getImplementation(cusMethod), method_getTypeEncoding(cusMethod));
            if (addSucc) {
                class_replaceMethod(selfClass, cusSEL, method_getImplementation(oriMethod), method_getTypeEncoding(oriMethod));
            }else {
                method_exchangeImplementations(oriMethod, cusMethod);
            }
        }
      
    });
    
}
- (instancetype)ZB_initWithCoder:(NSCoder *)coder
{
    id obj = [self ZB_initWithCoder:coder];
    if (obj) {
         [self setValue:kColor_8997B0 forKeyPath:@"_placeholderLabel.textColor"];
    }
    return  obj;
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來就是把...
    Dove_iOS閱讀 27,211評(píng)論 30 472
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,301評(píng)論 25 708
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,241評(píng)論 4 61
  • 剛剛看了電影頻道的《大唐玄奘》,看到一處情節(jié):在西域龜茲國蔥綠的草原上,近處繁華盛景駿馬撲蹄,遠(yuǎn)處崇山峻嶺巍峨蒼...
    阿兔_阿狗閱讀 158評(píng)論 1 0
  • 新年伊始,浙江衛(wèi)視的《二十四小時(shí)》第二季開播。 去年參加湖南衛(wèi)視《爸爸去哪兒》第四季意外爆紅的董力新近加盟“二十四...
    粒粒藍(lán)雪閱讀 3,445評(píng)論 16 13