1、在ios13前可直接调用系统自带key值“_placeholderLabel.textColor”设置
textField.placeholder =@"输入密码";
[textField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
2、ios13以及之后版本,直接使用“_placeholderLabel.textColor”会崩溃,可以使用富文本实现,颜色对应key参数是NSForegroundColorAttributeName
NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
style.minimumLineHeight = 0;
NSAttributedString *attributedString = [[NSAttributedString alloc] initWithString@"输入密码" attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:13], NSParagraphStyleAttributeName : style,NSForegroundColorAttributeName:[UIColor whiteColor]}];
textField.attributedPlaceholder = attributedString;
网友评论