- 方法一
使用 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];