Change the font size of UISearchBar

前端 未结 19 1181
情歌与酒
情歌与酒 2020-12-04 15:08

How can I change the font size of UISearchBar ?

相关标签:
19条回答
  • 2020-12-04 16:08

    You can use KVC (key-Value Coding) to get the textfield

    UITextField *textField = [self.searchBar valueForKey: @"_searchField"];
    [textField setTextColor:[UIColor redColor]];
    [textField setFont:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
    
    0 讨论(0)
  • 2020-12-04 16:08

    UITextField appearance does not work for UISearchBars created programmatically. You have to use a recursive workaround

    - (void)viewDidLoad {
      [super viewDidLoad];
      [self recursivelySkinTextField:self.searchBar];
    }
    
    - (void)recursivelySkinTextField:(UIView *)view {
      if (!view.subviews.count) return;
    
      for (UIView *subview in view.subviews) {
        if ([subview isKindOfClass:[UITextField class]]) {
          UITextField *searchField = (UITextField *)subview;
          searchField.font = [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] font];
          searchField.textColor = [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] textColor];
        }
        [self recursivelySkinTextField:subview];
      }
    }
    
    0 讨论(0)
  • 2020-12-04 16:08

    For Swift 4.2+

    let defaultTextAttributes = [
        NSAttributedString.Key.font: UIFont.init(name: "Ubuntu-Regular", size: 16),
        NSAttributedString.Key.foregroundColor: UIColor.gray
    ]
    UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = defaultTextAttributes
    
    0 讨论(0)
  • 2020-12-04 16:09

    You shouldn't use private API like in other answers. If you want to customise search bars in whole application, you can use class’s appearance proxy. It allows to modify search bar text attributes, placeholder attributes, cancel button, text field background image, background image etc. Example of use:

    if #available(iOS 9.0, *) {
        let searchBarTextAttributes = [
            NSFontAttributeName: UIFont.boldSystemFont(ofSize: 12), 
            NSForegroundColorAttributeName: UIColor.red
        ]
        // Default attributes for search text
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
    
        // Attributed placeholder string
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search...", attributes: searchBarTextAttributes)
    
        // Search bar cancel button
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes(searchBarTextAttributes, for: .normal)
        UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).title = "Cancel"
    }
    
    0 讨论(0)
  • 2020-12-04 16:09

    Try finding the search bar by its key:

    UITextField *searchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
    searchField.font = [[UIFont fontWithName:@"Oswald" size:11];
    
    0 讨论(0)
  • 2020-12-04 16:14

    I suggest yet a different option for iOS 5.0 and up:

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont systemFontOfSize:14]];
    

    for iOS 8 (as linked by Mike Gledhill):

    [[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
                NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
          }];
    

    for iOS 9 and above:

    [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20]}];
    

    This way you don't need to mess with enumerating subviews for every search bar in your app.

    0 讨论(0)
提交回复
热议问题