SearchBar 修改Font 和 占位 文字 颜色 大小

UISearchBar 的基础设置:

  • (void)setBarButtonItem
    {
    //隐藏导航栏上的返回按钮
    [self.navigationItem setHidesBackButton:YES];
    //用来放searchBar的View
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(5, 7, self.view.frame.size.width, 30)];
    //创建searchBar
    UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(titleView.frame) - 15, 30)];
    //默认提示文字
    searchBar.placeholder = @"搜索内容";
    //背景图片
    searchBar.backgroundImage = [UIImage imageNamed:@"clearImage"];
    //代理
    searchBar.delegate = self;
    //显示右侧取消按钮
    searchBar.showsCancelButton = YES;
    //光标颜色
    searchBar.tintColor = UIColorFromRGB(0x595959);
    //拿到searchBar的输入框
    UITextField *searchTextField = [searchBar valueForKey:@"_searchField"];
    //字体大小
    searchTextField.font = [UIFont systemFontOfSize:15];
    //输入框背景颜色
    searchTextField.backgroundColor = [UIColor colorWithRed:234/255.0 green:235/255.0 blue:237/255.0 alpha:1];
    //拿到取消按钮
    UIButton *cancleBtn = [searchBar valueForKey:@"cancelButton"];
    //设置按钮上的文字
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    //设置按钮上文字的颜色
    [cancleBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [titleView addSubview:searchBar];
    self.searchBar = searchBar;
    self.navigationItem.titleView = titleView;
    }

代理方法:

pragma mark - UISearchBarDelegate

  • (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    return YES;
    }

  • (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    searchBar.showsCancelButton = YES;
    }

  • (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
    {
    NSLog(@"SearchButton");
    }

  • (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
    {
    [self.searchBar resignFirstResponder];
    [self.navigationController popViewControllerAnimated:YES];
    }

  • (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
    {
    searchBar.showsCancelButton = YES;
    }

  • (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
    NSString *inputStr = searchText;
    [self.results removeAllObjects];
    for (ElderModel *model in self.dataArray) {
    if ([model.name.lowercaseString rangeOfString:inputStr.lowercaseString].location != NSNotFound) {
    [self.results addObject:model];
    }
    }
    [self.tableView reloadData];
    }

    // 找到searchbar的searchField属性
    UITextField *searchField = [self.searchBar valueForKey:@"searchField"];
    if (searchField) {
    // 背景色
    [searchField setBackgroundColor:[UIColor colorWithRed:0.074 green:0.649 blue:0.524 alpha:1.000]];
    // 设置字体颜色 & 占位符 (必须)
    searchField.textColor = [UIColor whiteColor];
    searchField.placeholder = @"placeholder";
    // 根据@"_placeholderLabel.textColor" 找到placeholder的字体颜色
    [searchField setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"];
    // 圆角
    searchField.layer.cornerRadius = 10.0f;
    searchField.layer.masksToBounds = YES;

    }

// searchBar 图标设置
[self.searchBar setImage:[UIImage imageNamed:@"magnifier"] forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];

你可能感兴趣的:(SearchBar 修改Font 和 占位 文字 颜色 大小)