iOS改变searchBar搜索栏placeholder的字体颜色

如题所示,就是一个小tip,百度了很久都没答案,就在stackoverflow上找到了答案,方法有如下几种:

1.iOS5+ oc版:

[[UILabelappearanceWhenContainedIn:[UISearchBarclass],nil]setTextColor:[UIColorredColor]];

2.取出searcher里的textfield,再进行修改:

//取出textfield

UITextField*searchField=[searchBar valueForKey:@"_searchField"];

//改变searcher的textcolor

searchField.textColor=[UIColor redColor];

//改变placeholder的颜色

[searchField setValue:[UIColorblueColor]forKeyPath:@"_placeholderLabel.textColor"];

3.swift版

var  textFieldInsideSearchBar=searchBar.valueForKey("searchField")as?UITextField

textFieldInsideSearchBar?.textColor=UIColor.whiteColor()   

var  textFieldInsideSearchBarLabel=textFieldInsideSearchBar!.valueForKey("placeholderLabel")as?UILabel

textFieldInsideSearchBarLabel?.textColor=UIColor.whiteColor()

以上内容参考ios - UISearchBar change placeholder color - Stack Overflow

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容