NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor redColor];
UITextField *textField = [[UITextField alloc]init];
textField.attributedPlaceholder = [[NSMutableAttributedString alloc]initWithString:@"请输入内容" attributes:attrs];
还有一个更省力的方法:
[textField setValue:[UIColor yellowColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont systemFontOfSize:17] forKeyPath:@"_placeholderLabel.font"];
改变光标颜色(此方法影响所有TextField):
[[UITextField appearance] setTintColor:[UIColor blackColor]];
更新:
iOS13禁止访问私有属性了,可以通过attributedPlaceholder属性实现样式修改
NSMutableAttributedString *placeholderString = [[NSMutableAttributedString alloc] initWithString:textField.placeholder attributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
textField.attributedPlaceholder = placeholderString;