设置UITextField占位符的颜色和字体

 

 

今天刚学了UITextField控件, 感觉在里面设置占位符非常好, 给用户提示信息, 于是就在想占位符的字体和颜色能不能改变呢?

下面是小编的一些简单的实现.

主要有两种方法:

方法1:利用富文本

@property (weak, nonatomic) IBOutlet UITextField *textField;

 NSDictionary *dic = @{NSForegroundColorAttributeName:[UIColor magentaColor], NSFontAttributeName:[UIFont systemFontOfSize:15]};

 self.textField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"欢迎回来" attributes:dic];

 self.textField.tintColor = [UIColor cyanColor];

方法2:KVC

self.textField.placeholder = @"欢迎回来!";

 [self.textField setValue:[UIColor magentaColor] forKeyPath:@"_placeholderLabel.textColor"]

 [self.textField setValue:[UIFont systemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];

 self.textField.tintColor = [UIColor greenColor];

KVC功能很多呦, 还可以实现数组简单的求和

NSArray *dataArray = @[@"1", @"2", @"3", @"4", @"5"];

 NSNumber *sum = [dataArray valueForKeyPath:@"@sum.integerValue"];

 NSLog(@"%@", sum);

 
 
原文地址:https://www.cnblogs.com/hsxblog/p/5140368.html