iOS UISearchBar修改placeholder字型顏色和大小

來源:互聯網
上載者:User

在修改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];

//之前的效果

//之後的效果

其他:

#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");}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.