UISearchBar设置placeholder文字大小颜色

self.searchBar= [[UISearchBaralloc]initWithFrame:CGRectMake(20,20, kUISCREEN_WIDTH -40,30)];    _searchBar.placeholder= @"个合格";    _searchBar.tintColor= [UIColorwhiteColor];    _searchBar.translucent=YES;    _searchBar.layer.masksToBounds=YES;    _searchBar.layer.cornerRadius=5.0;    _searchBar.alpha=0.2;//添加背景图,可以去掉外边框的灰色部分[_searchBar setBackgroundImage:[UIImagenew]];    [_searchBar setTranslucent:YES];//这个枚举可以对searchBar进行修改_searchBar.searchBarStyle= UISearchBarStyleProminent;//之前的效果,如下面的第一个效果图//加上如下命令效果如下searchBar.barTintColor= [UIColorwhiteColor];//给searchBar中的textField添加背景图[_searchBar setSearchFieldBackgroundImage:[UIImageimageNamed:@"backgroundImage"] forState:UIControlStateNormal];//一下代码为修改placeholder字体的颜色和大小UITextField * searchField = [_searchBar valueForKey:@"_searchField"];    [searchField setValue:[UIColorredColor] forKeyPath:@"_placeholderLabel.textColor"];    [searchField setValue:[UIFontboldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];    [topBackImageView addSubview:self.searchBar];

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

//之前的效果

UISearchBar设置placeholder文字大小颜色_第1张图片

//之后的效果

UISearchBar设置placeholder文字大小颜色_第2张图片

其他:

#import"ViewController.h"@interfaceViewController()@property(nonatomic,strong)UISearchBar* searchBar;@end@implementationViewController- (void)viewDidLoad {    [superviewDidLoad];self.searchBar= [[UISearchBaralloc]initWithFrame:CGRectMake(20,80,300,50)];//默认白色搜索框,多出的背景为灰色;UIBarStyleDefault 默认 UIBarStyleBlack背景为黑色_searchBar.barStyle= UIBarStyleDefault;//设置搜索框整体的风格为不显示背景,默认为Prominent显示_searchBar.searchBarStyle= UISearchBarStyleDefault;//设置搜索框的文字_searchBar.text= @"搜索框";//显示在searchBar顶部的一行文字//    _searchBar.prompt = @"prompt";//占位符_searchBar.placeholder= @"占位符";//设置搜索框中的光标的颜色为黄色_searchBar.tintColor= [UIColoryellowColor];//设置搜索框的背景颜色_searchBar.barTintColor= [UIColorredColor];//设置是否透明_searchBar.translucent=YES;    _searchBar.showsCancelButton=YES;    _searchBar.showsBookmarkButton=YES;//设置搜索框textField的位置,其他控件位置不改变_searchBar.searchFieldBackgroundPositionAdjustment= UIOffsetMake(50,0);//设置textField里面文字在field中的位置_searchBar.searchTextPositionAdjustment= UIOffsetMake(50,0);//自定义搜索框放大镜的图标[_searchBar setImage:[UIImageimageNamed:@"1"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];//设置bookMark图标的设置[_searchBar setImage:[UIImageimageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];    _searchBar.delegate=self;    [self.viewaddSubview:self.searchBar];}#pragma mark - UISearchBarDelegate/*

调用BookmarkButton的点击方法,需要先设置showsBookmarkButton = YES,并且showsSearchResultsButton 不能同时设置为yes,否则不会显示BookmarkButton,导致无法调用方法

//是否在搜索框右侧显示一个图书的按钮,默认为NO,

_searchBar.showsBookmarkButton = YES;

调用ResultsListButton的点击方法,设置showsSearchResultsButton = YES;

//当搜索框将要开始使用时调用。yes表示搜索框可以使用,默认为yes否则搜索框无法使用

_searchBar.showsSearchResultsButton = YES;

*/- (BOOL)searchBarShouldBeginEditing:(UISearchBar*)searchBar{NSLog(@"ShouldBegin");returnYES;}//当搜索框开始编辑时候调用- (void)searchBarTextDidBeginEditing:(UISearchBar*)searchBar{NSLog(@"DidBegin");}//当搜索框将要将要结束使用时调用。- (BOOL)searchBarShouldEndEditing:(UISearchBar*)searchBar{NSLog(@"ShouldEnd");returnYES;}//当搜索框结束编辑时候调用- (void)searchBarTextDidEndEditing:(UISearchBar*)searchBar{NSLog(@"DidEnd");}//当field里面内容改变时候就开始掉用。- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText{NSLog(@"DidChange");if(searchText !=nil&& searchText.length>0){        [self.searchDataAryremoveAllObjects];for(SearchModel * model inself.originAry) {if([model.shop_namerangeOfString:searchText options:NSCaseInsensitiveSearch].length>0) {                [self.searchDataAryaddObject:model];            }        }        [self.tableViewreloadData];    }else{self.searchDataAry= [NSMutableArrayarrayWithArray:self.originAry];        [self.tableViewreloadData];    }}//在field里面输入时掉用,询问是否允许输入,yes表示允许,默认为yes,否则无法输入- (BOOL)searchBar:(UISearchBar*)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString*)text {NSLog(@"shouldChange");returnYES;}//点击SearchButton调用- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar{NSLog(@"SearchButtonClicked");}//点击BookmarkButton调用- (void)searchBarBookmarkButtonClicked:(UISearchBar*)searchBar {NSLog(@"BookmarkButtonClicked");}//点击CancelButton调用- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar {NSLog(@"CancelButton");}//点击ResultsListButton调用- (void)searchBarResultsListButtonClicked:(UISearchBar*)searchBar{NSLog(@"ResultsListButton");}

你可能感兴趣的:(UISearchBar设置placeholder文字大小颜色)