玩轉(zhuǎn)iOS中UITextField的placeholder顏色

珍惜時(shí)間

UITextField是iOS開發(fā)中經(jīng)常使用到的控件,它有一個(gè)placeholder屬性,也就是占位文字。默認(rèn)占位文字顏色是70% gray,但有時(shí)我們可能需要修改其占位文字的顏色,下文中將為大家介紹三中修改方法,并就動(dòng)態(tài)改變顏色做相關(guān)說(shuō)明(關(guān)于動(dòng)態(tài)改變:當(dāng)UITextField為第一響應(yīng)者時(shí)為一種顏色,取消第一響應(yīng)者時(shí)為另一種顏色)。

方法1

  • 設(shè)置 attributedPlaceholder屬性
  • 說(shuō)明:此種方式對(duì)于無(wú)需動(dòng)態(tài)改變placeholder顏色較為方便。

  • 代碼1(單色)
NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 創(chuàng)建屬性字典
  attrs[NSFontAttributeName] = [UIFont systemFontOfSize:17]; // 設(shè)置font
  attrs[NSForegroundColorAttributeName] = [UIColor greenColor]; // 設(shè)置顏色
  NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"夏蟲不可以語(yǔ)冰" attributes:attrs]; // 初始化富文本占位字符串
  self.textField.attributedPlaceholder = attStr;
  • 效果1


    單色placeholder
  • 代碼2(復(fù)色)

NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"夏蟲不可以語(yǔ)冰"];
 [attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor redColor],
                            NSFontAttributeName : [UIFont systemFontOfSize:15.0]} range:NSMakeRange(0, 2)];
 [attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor greenColor],
                            NSFontAttributeName : [UIFont systemFontOfSize:17.0]} range:NSMakeRange(2, 3)];
 [attStr setAttributes:@{NSForegroundColorAttributeName : [UIColor blueColor],
                            NSFontAttributeName : [UIFont systemFontOfSize:15.0]} range:NSMakeRange(5, 2)];
    self.textField.attributedPlaceholder = attStr;
  • 效果


    復(fù)色placeholder

方法2

  • 自定義UITextField,重寫- (void)drawPlaceholderInRect:(CGRect)rect;
  • 說(shuō)明:此種方式只能設(shè)置一次狀態(tài),不能動(dòng)態(tài)的改變placeholder的顏色,但可以設(shè)置placeholder所在位置。

  • 代碼
- (void)drawPlaceholderInRect:(CGRect)rect
{
    [self.placeholder drawInRect:CGRectMake(0, 2, rect.size.width, 25) withAttributes:@{ NSFontAttributeName: [UIFont systemFontOfSize:16.0],
                                        NSForegroundColorAttributeName : [UIColor blueColor],
                                     }];
}
  • 效果


    placeholder

方法3

  • 自定義UITextField,利用runTime找出UITextFiled內(nèi)部隱藏的成員變量和屬性,利用KVC進(jìn)行修改。
  • 說(shuō)明:此種方式對(duì)于動(dòng)態(tài)改變placeholder顏色較為方便。

  • 拓展代碼(利用runTime找出成員變量和屬性,程序中無(wú)需使用,只是幫助我們看清UITextField內(nèi)部結(jié)構(gòu),知道其中的相關(guān)成員變量和屬性,然后賦值即可)。
#import "SJTextField.h"
#import <objc/runtime.h>
@implementation SJTextField

// 初始化調(diào)用一次 用于查看UITextField中的成員屬性和變量
+ (void)initialize
{
    [self getIvars];
    // [self getProperties];
}

// 獲取所有屬性
+ (void)getProperties
{
    unsigned int count = 0;
    objc_property_t *properties = class_copyPropertyList([UITextField class], &count);
    for (int i = 0; i<count; i++) {
        // 取出屬性
        objc_property_t property = properties[i];
        
        // 打印屬性名字
        NSLog(@"%s<---->%s", property_getName(property), property_getAttributes(property));
    }
    free(properties);
}

// 獲取所有成員變量
+ (void)getIvars
{
    unsigned int count = 0;
    // 拷貝出所有的成員變量列表
    Ivar *ivars = class_copyIvarList([UITextField class], &count);
    for (int i = 0; i<count; i++) {
        // 取出成員變量
        //        Ivar ivar = *(ivars + i);
        Ivar ivar = ivars[i];
        
        // 打印成員變量名字
        NSLog(@"%s %s", ivar_getName(ivar), ivar_getTypeEncoding(ivar));
    }
    // 釋放
    free(ivars);
}
@end
  • 拓展點(diǎn) 可以查到有兩個(gè)關(guān)于placeholder的屬性和變量,分別是_placeholderLabel.textColor_placeholderLabel,故下面就是用來(lái)設(shè)置動(dòng)態(tài)改變placeholder顏色的代碼。
  • 代碼(在自定義UITextField中)
#import "SJTextField.h"
#import <objc/runtime.h>
@implementation SJTextField
static NSString * const SJPlacerholderColorKeyPath = @"_placeholderLabel.textColor";
- (void)awakeFromNib
{
// 設(shè)置placeholder開始顏色(方式一)
//    UILabel *placeholderLabel = [self valueForKeyPath:@"_placeholderLabel"];
//    placeholderLabel.textColor = [UIColor redColor];   
   // 設(shè)置placeholder開始顏色(方式二)
    [self setValue:[UIColor greenColor] forKeyPath:SJPlacerholderColorKeyPath];
    // 不成為第一響應(yīng)者
    [self resignFirstResponder];
}

/**
 * 當(dāng)前文本框聚焦時(shí)就會(huì)調(diào)用
 */
- (BOOL)becomeFirstResponder
{
    // 修改占位文字顏色
    [self setValue:[UIColor redColor] forKeyPath:SJPlacerholderColorKeyPath];
    return [super becomeFirstResponder];
}

/**
 * 當(dāng)前文本框失去焦點(diǎn)時(shí)就會(huì)調(diào)用
 */
- (BOOL)resignFirstResponder
{
    // 修改占位文字顏色
    [self setValue:[UIColor greenColor] forKeyPath:SJPlacerholderColorKeyPath];
    return [super resignFirstResponder];
}
@end
  • 效果
未進(jìn)入編輯狀態(tài)
進(jìn)入編輯狀態(tài)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容