iOS 知识点

对 iOS 中 placeholder 占位文字的几种操作

2016-11-02  本文已影响1025人  magic_pill

一、对 UITextField 的 placeholder 占位文字进行赋值

- (void)viewDidLoad {
    [super viewDidLoad];
    self.textField.placeholder = @"你是谁 呼哈哈哈~";
}
赋值效果

二、对 UITextField 的 attributedPlaceholder 进行赋值

2.1.通过 NSAttributedString 实现 - 01
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //带属性的文字(富文本技术)
    NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"请输入手机号"];
    
    self.textField.attributedPlaceholder  = placeholder;
}
NSAttributedString-01
2.2.通过 NSAttributedString 实现 - 02
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置文字属性
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:16];
    attrs[NSForegroundColorAttributeName] = [UIColor yellowColor];
    
    //带属性的文字(富文本技术)
    NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"输入手机号啦" attributes:attrs];
    self.textField.attributedPlaceholder  = placeholder;
}
NSAttributedString-02
2.3.通过 NSMutableAttributedString 实现更多炫酷效果
- (void)viewDidLoad {
    [super viewDidLoad];

    //带属性的文字(富文本技术)
    NSMutableAttributedString *placeholder = [[NSMutableAttributedString alloc] initWithString:@"手机号码"];
    
    [placeholder setAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:12],
        NSForegroundColorAttributeName:[UIColor blueColor]
                                } range:NSMakeRange(0, 1)];

    [placeholder setAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:16],
        NSForegroundColorAttributeName:[UIColor redColor]
                                } range:NSMakeRange(1, 1)];
    
    [placeholder setAttributes:@{
        NSFontAttributeName:[UIFont systemFontOfSize:14],
        NSForegroundColorAttributeName:[UIColor yellowColor]
                                }range:NSMakeRange(2, 1)];
    
    [placeholder setAttributes:@{
        NSUnderlineStyleAttributeName:[NSNumber numberWithInt:2]
                                 } range:NSMakeRange(3, 1)];
    
    self.textField.attributedPlaceholder  = placeholder;
}
NSMutableAttributedString

三、通过重写 textField 的 - (void)drawPlaceholderInRect:(CGRect)rect 方法

自定义一个继承于 UITextField 的类 YJWTextField,然后将 textField 定义为 YJWTextField 类型

定义类型
#import "YJWTextField.h"

@implementation YJWTextField

-(void)drawPlaceholderInRect:(CGRect)rect
{
    [self.placeholder drawInRect:CGRectMake(0, 10, rect.size.width, 25) withAttributes:@{
        NSForegroundColorAttributeName:[UIColor purpleColor]
        }];
}

@end
效果图

四、通过运行时 Runtime 来设置 placeholder 占位文字的高亮颜色

4.1.运行时 Runtime 的简单使用
#import "YJWTextField.h"
#import <objc/runtime.h>

@implementation YJWTextField

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    unsigned int count = 0;
    //copy 所有的成员变量列表
    Ivar *vars = class_copyIvarList([UITextField class], &count);
    for (int i=0; i<count; i++) {
        //取出成员变量
        Ivar valu = *(vars + i);
        YJWLog(@"%s",ivar_getName(valu));
    }
    //释放
    free(vars);
}

@end

通过运行时获得 UITextField 的所有成员变量:

UITextField的属性

其中有60个成员变量,包含有 _placeholderLabel 成员变量:

_placeholderLabel
4.2.通过 Runtime 设置 placeholder 占位文字颜色为红色
-(void)awakeFromNib
{
    [super awakeFromNib];

    UITextField *t = [self valueForKey:@"_placeholderLabel"];
    t.textColor = [UIColor redColor];
}

runtime设置
4.3.最终效果实现:设置 textLabel 未选中时显示亮灰色,选中时显示白色
#import "YJWTextField.h"
#import <objc/runtime.h>

#define YJWColorKeyPath @"_placeholderLabel.textColor"

@implementation YJWTextField

-(void)awakeFromNib
{
    [super awakeFromNib];
    
    //设置初始时,占位文字颜色为 lightGrayColor
    [self resignFirstResponder];
    //设置光标颜色和文字颜色一样
    self.tintColor = self.textColor;
}

-(BOOL)becomeFirstResponder
{
    [super becomeFirstResponder];
    //设置高亮时,占位文字颜色为白色
    [self setValue:[UIColor whiteColor] forKeyPath:YJWColorKeyPath];
    return YES;
}

-(BOOL)resignFirstResponder
{
    [super resignFirstResponder];
    //设置非高亮状态下,占位文字颜色为 lightGrayColor
    [self setValue:[UIColor lightGrayColor] forKeyPath:YJWColorKeyPath];
    return YES;
}

@end
指定类型
上一篇 下一篇

猜你喜欢

热点阅读