玩轉iOS中UITextField的placeholder顏色

ieyong 8年前發布 | 22K 次閱讀 iOS開發 移動開發 UITextField

珍惜時間

UITextField 是iOS開發中經常使用到的控件,它有一個 placeholder 屬性,也就是占位文字。默認占位文字顏色是 70% gray ,但有時我們可能需要修改其占位文字的顏色,下文中將為大家介紹三中修改方法,并就動態改變顏色做相關說明(關于動態改變:當UITextField為第一響應者時為一種顏色,取消第一響應者時為另一種顏色)。

方法1

  • 設置 attributedPlaceholder 屬性
  • 說明:此種方式對于無需動態改變 placeholder 顏色較為方便。
  • 代碼1(單色)
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; // 創建屬性字典
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:17]; // 設置font
    attrs[NSForegroundColorAttributeName] = [UIColor greenColor]; // 設置顏色
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:@"夏蟲不可以語冰" attributes:attrs]; // 初始化富文本占位字符串
    self.textField.attributedPlaceholder = attStr;
  • 效果1

    單色placeholder

  • 代碼2(復色)

    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:@"夏蟲不可以語冰"];
    [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;
  • 效果

    復色placeholder

方法2

  • 自定義UITextField,重寫 - (void)drawPlaceholderInRect:(CGRect)rect;
  • 說明:此種方式只能設置一次狀態,不能動態的改變 placeholder 的顏色,但可以設置 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內部隱藏的成員變量和屬性,利用KVC進行修改。
  • 說明:此種方式對于動態改變 placeholder 顏色較為方便。
  • 拓展代碼 (利用runTime找出成員變量和屬性,程序中無需使用,只是幫助我們看清UITextField內部結構,知道其中的相關成員變量和屬性,然后賦值即可)。
#import "SJTextField.h"
#import <objc/runtime.h>
@implementation SJTextField

// 初始化調用一次 用于查看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
  • 拓展點 可以查到有兩個關于 placeholder 的屬性和變量,分別是 _placeholderLabel.textColor 和 _placeholderLabel ,故下面就是用來設置動態改變 placeholder 顏色的代碼。
  • 代碼(在自定義UITextField中)
#import "SJTextField.h"
#import <objc/runtime.h>
@implementation SJTextField
static NSString * const SJPlacerholderColorKeyPath = @"_placeholderLabel.textColor";
- (void)awakeFromNib
{
// 設置placeholder開始顏色(方式一)
//    UILabel *placeholderLabel = [self valueForKeyPath:@"_placeholderLabel"];
//    placeholderLabel.textColor = [UIColor redColor];   
   // 設置placeholder開始顏色(方式二)
    [self setValue:[UIColor greenColor] forKeyPath:SJPlacerholderColorKeyPath];
    // 不成為第一響應者
    [self resignFirstResponder];
}

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

/**
 * 當前文本框失去焦點時就會調用
 */
- (BOOL)resignFirstResponder
{
    // 修改占位文字顏色
    [self setValue:[UIColor greenColor] forKeyPath:SJPlacerholderColorKeyPath];
    return [super resignFirstResponder];
}
@end
  • 效果

未進入編輯狀態

進入編輯狀態

 

來自:http://www.jianshu.com/p/de0f819a1576

 

 本文由用戶 ieyong 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!