最近項(xiàng)目中用到了系統(tǒng)自帶的UISearchBar,應(yīng)UI設(shè)計(jì)師要求需要修改placeholder的文字顏色共螺,網(wǎng)上全部都是幾年前的老版本了,但是iOS13之后蘋果要求不可以再使用秦效,否則會(huì)崩潰法梯。老版本如下:
//會(huì)崩潰
UITextField * searchField = [_searchBar valueForKey:@"_searchField"];
[searchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"]; [searchField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
使用新的方法夜惭,不會(huì)崩潰,拿來分享大家
新的修改方法如下:
//iOS13之后新方法
UITextField * searchField = [_homeSearchBar valueForKey:@"_searchTextField"];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"搜索關(guān)鍵詞" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRGBHex:0xA1A7A8 alpha:0.5],NSFontAttributeName:searchField.font}];
searchField.attributedPlaceholder = attrString;