iOS UISearchBar修改placeholder字体颜色和大小

在修改searchBar上面的placeholder字体颜色时,我自己手写的代码跟正确的一模一样时,它识别不出来,总是崩,错误内容说是没有那个value,真是见鬼了。当我粘贴过来时,它就好了。爱,真是那个什么了………………

 self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(20 , 20, kUISCREEN_WIDTH - 40, 30)];
    _searchBar.placeholder = @"个合格";
    _searchBar.tintColor = [UIColor whiteColor];
    _searchBar.translucent = YES;
    _searchBar.layer.masksToBounds = YES;
    _searchBar.layer.cornerRadius = 5.0;
    _searchBar.alpha = 0.2;
    //添加背景图,可以去掉外边框的灰色部分
    [_searchBar setBackgroundImage:[UIImage new]];
    [_searchBar setTranslucent:YES];

    //这个枚举可以对searchBar进行修改
    _searchBar.searchBarStyle = UISearchBarStyleProminent;
    //之前的效果,如下面的第一个效果图
    //加上如下命令效果如下
    searchBar.barTintColor = [UIColor whiteColor];
    //给searchBar中的textField添加背景图
   [_searchBar setSearchFieldBackgroundImage:[UIImage imageNamed:@"backgroundImage"] forState:UIControlStateNormal];
    //一下代码为修改placeholder字体的颜色和大小
    UITextField * searchField = [_searchBar valueForKey:@"_searchField"];
    [searchField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
    [searchField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@"_placeholderLabel.font"];
    [topBackImageView addSubview:self.searchBar];

//之前的效果
iOS UISearchBar修改placeholder字体颜色和大小_第1张图片
//之后的效果
iOS UISearchBar修改placeholder字体颜色和大小_第2张图片

其他:

#import "ViewController.h"

@interface ViewController ()<UISearchBarDelegate>

@property(nonatomic, strong)UISearchBar * searchBar;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.searchBar = [[UISearchBar alloc]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 = [UIColor yellowColor];
    //设置搜索框的背景颜色
    _searchBar.barTintColor = [UIColor redColor];
    //设置是否透明
    _searchBar.translucent = YES;

    _searchBar.showsCancelButton = YES;
    _searchBar.showsBookmarkButton = YES;

    //设置搜索框textField的位置,其他控件位置不改变
    _searchBar.searchFieldBackgroundPositionAdjustment = UIOffsetMake(50, 0);
    //设置textField里面文字在field中的位置
    _searchBar.searchTextPositionAdjustment = UIOffsetMake(50, 0);

    //自定义搜索框放大镜的图标
    [_searchBar setImage:[UIImage imageNamed:@"1"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

    //设置bookMark图标的设置
    [_searchBar setImage:[UIImage imageNamed:@"2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];

    _searchBar.delegate = self;

    [self.view addSubview: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");
    return YES;
}
//当搜索框开始编辑时候调用
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    NSLog(@"DidBegin");

}
//当搜索框将要将要结束使用时调用。
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    NSLog(@"ShouldEnd");

    return YES;
}
//当搜索框结束编辑时候调用
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    NSLog(@"DidEnd");
}

//当field里面内容改变时候就开始掉用。
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    NSLog(@"DidChange");
    if(searchText != nil && searchText.length > 0){
        [self.searchDataAry removeAllObjects];
        for (SearchModel * model in self.originAry) {
            if ([model.shop_name rangeOfString:searchText options:NSCaseInsensitiveSearch].length > 0) {
                [self.searchDataAry addObject:model];
            }
        }
        [self.tableView reloadData];
    }else{
        self.searchDataAry = [NSMutableArray arrayWithArray:self.originAry];
        [self.tableView reloadData];
    }


}
//在field里面输入时掉用,询问是否允许输入,yes表示允许,默认为yes,否则无法输入
- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    NSLog(@"shouldChange");
    return YES;
}
//点击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");
}

你可能感兴趣的:(iOS开发笔记)