码迷,mamicode.com
首页 > 其他好文 > 详细

文本编辑框光标颜色和占位文字颜色自定义

时间:2017-05-14 18:54:21      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:table   blog   tco   ted   val   event   label   bsp   文本编辑   

1.自定义一个自己的UITextField类,在类中实现如下代码:

方法一:利用UITextField属性attributedPlaceholder直接设置

-(void)awakeFromNib{

    [super awakeFromNib];
    //光标颜色
    self.tintColor = [UIColor whiteColor];
    //占位文字颜色
    [self addTarget:self action:@selector(startEditTextField) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(endEditTextField) forControlEvents:UIControlEventEditingDidEnd];
    
}

-(void)startEditTextField{
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[NSForegroundColorAttributeName] = [UIColor whiteColor];
    
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
}

-(void)endEditTextField{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
    dict[NSForegroundColorAttributeName] = [UIColor grayColor];
    
    self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.placeholder attributes:dict];
}

方法二:利用用KVC获取TextField系统属性设置

-(void)awakeFromNib{

    [super awakeFromNib];
    //光标颜色
    self.tintColor = [UIColor whiteColor];
    //占位文字颜色
    [self addTarget:self action:@selector(startEditTextField) forControlEvents:UIControlEventEditingDidBegin];
    [self addTarget:self action:@selector(endEditTextField) forControlEvents:UIControlEventEditingDidEnd];
    
    
}

-(void)startEditTextField{
    
    UILabel *placeholderLabel = [self valueForKey:@"placeholderLabel"];//方法实现:首先找有没有这样的get方法,没有则继续找placeholderLabel这个成员属性,还没有则接着找_placeholderLabel
    placeholderLabel.textColor = [UIColor whiteColor];

    
}

-(void)endEditTextField{
    
    UILabel *placeholderLabel = [self valueForKey:@"placeholderLabel"];
    placeholderLabel.textColor = [UIColor grayColor];
}

 

文本编辑框光标颜色和占位文字颜色自定义

标签:table   blog   tco   ted   val   event   label   bsp   文本编辑   

原文地址:http://www.cnblogs.com/sivek/p/6853201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!