如何更改UISearchBar占位符和图像色调颜色?

如何更改UISearchBar占位符和图像色调颜色?,第1张

如何更改UISearchBar占位符和图像色调颜色

如果您有可以使用的自定义图像,则可以使用类似于以下内容的方式设置图像并更改占位符文本颜色:

[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)    }


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5476697.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存