項目中經(jīng)常需要修改placeholder顏色,默認的是很淺的吐限,經(jīng)常在登錄界面等處和需求不符合鲜侥。正好今天寫項目遇到,需要設置成白色诸典,經(jīng)過我測試描函,有2種方法修改較為簡單。(還有種runtime查找的方法不建議使用)
1.KVC的方式修改
[textfield setValue:[UIColorgrayColor]forKeyPath:@"_placeholderLabel.textColor"];
2.利用富文本NSAttributedString方式設置placeholder
NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
attrs[NSForegroundColorAttributeName] = [UIColor whiteColor];
NSAttributedString *placeholder = [[NSAttributedString alloc] initWithString:@"請輸入用戶名" attributes:attrs];
self.accountTextField.attributedPlaceholder = placeholder;
最后,我采用了第二種方式舀寓。這種方式侵入性較小胆数,并且無副作用,推薦使用互墓。