.h 中類方法?
+ (void)setPlaceholderForTextField:(UITextField*)textFieldwithFont:(UIFont*)fontandColor:(UIColor*)textColorandPlaceholderText:(NSString*)placeholder;
.m 中實現(xiàn)
+ (void)setPlaceholderForTextField:(UITextField*)textFieldwithFont:(UIFont*)fontandColor:(UIColor*)textColorandPlaceholderText:(NSString*)placeholder{
? ? NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:textColor,NSFontAttributeName:font}];
? ? textField.attributedPlaceholder= attrString;
}
使用方法
*UISearchBar中有searchTextField 屬性
? [MyUtil setPlaceholderForTextField:_searchBar.searchTextField withFont:[UIFont systemFontOfSize:13] andColor:[UIColor greenColor] andPlaceholderText:@"提示內(nèi)容"];