如果您有可以使用的自定义图像,则可以使用类似于以下内容的方式设置图像并更改占位符文本颜色:
[searchBar setImage:[UIImage imageNamed:@"SearchWhite"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];UITextField *searchTextField = [searchBar valueForKey:@"_searchField"]; if ([searchTextField respondsToSelector:@selector(setAttributedPlaceholder:)]) { UIColor *color = [UIColor purpleColor]; [searchTextField setAttributedPlaceholder:[[NSAttributedString alloc] initWithString:@"Search" attributes:@{NSForegroundColorAttributeName: color}]];}
在该示例中,我使用了PurpleColor,而是可以使用该
+ (UIColor *)colorWithRed:(CGFloat)redgreen:(CGFloat)green blue:(CGFloat)blue alpha:(CGFloat)alpha方法来创建自定义的深绿色。
编辑:我刚刚意识到你正在迅速写它…嗯。快速输入答案,这样我就不会只把答案留在Obj-C中了。
searchBar.setImage(UIImage(named: "SearchWhite"), forSearchBarIcon: UISearchBarIcon.Search, state: UIControlState.Normal); var searchTextField: UITextField? = searchBar.valueForKey("searchField") as? UITextField if searchTextField!.respondsToSelector(Selector("attributedPlaceholder")) { var color = UIColor.purpleColor() let attributeDict = [NSForegroundColorAttributeName: UIColor.purpleColor()] searchTextField!.attributedPlaceholder = NSAttributedString(string: "search", attributes: attributeDict) }
斯威夫特3.0
var searchTextField: UITextField? = searchBar.value(forKey: "searchField") as? UITextField if searchTextField!.responds(to: #selector(getter: UITextField.attributedPlaceholder)) { let attributeDict = [NSForegroundColorAttributeName: UIColor.white] searchTextField!.attributedPlaceholder = NSAttributedString(string: "Search", attributes: attributeDict) }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)