- 方法一
使用 KVC 方式自定义 textField 的占位文本.
示例代码
UITextField *tf = [[UITextField alloc] init];
tf.placeholder = @“占位文本”;
//textColor 和 textFont 为自定义的字体颜色字体尺寸
[tf setValue:textColor forKeyPath:@"_placeholderLabel.textColor"];
[tf setValue:[UIFont boldSystemFontOfSize:textFont] forKeyPath:@"_placeholderLabel.font"];
- 方法二
使用官方提供的 API 自定义 textFIeld
@property(nullable, nonatomic,copy) NSAttributedString *attributedPlaceholder NS_AVAILABLE_IOS(6_0); // default is nil
示例代码
UITextField *tf = [[UITextField alloc] init];
tf.placeholder = @"占位文本";
NSMutableDictionary *dictM = [NSMutableDictionary dictionary];
dictM[NSForegroundColorAttributeName] = [UIColor redColor];
dictM[NSFontAttributeName] = [UIFont systemFontOfSize:20];
NSAttributedString *attribute = [[NSAttributedString alloc] initWithString:tf.placeholder attributes:dictM];
[tf setAttributedPlaceholder:attribute];