Change the font size of UISearchBar Change the font size of UISearchBar ios ios

Change the font size of UISearchBar


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.


The safe way for performing this operation is as follows:

for(UIView *subView in searchBar.subviews) {    if ([subView isKindOfClass:[UITextField class]]) {        UITextField *searchField = (UITextField *)subView;        searchField.font = [UIFont fontWithName:@"Oswald" size:11];    }}

Why is this safer than the accepted answer? Because it doesn't rely on the index of the UITextField staying constant. (it's also a cleaner for loop)


The accepted answer is not the recommended way of applying font for UISearchBar. Better to avoid this approach and use the answers provided by @rafalkitta or @josema.

But be cautious, this will be applied to all the search bars through out the app. In order to apply this approach to a particular search bar: create a subclass of UISearchBar and set the defaultTextAttributes on the UITextField like below

Swift4:

    let defaultTextAttribs = [NSAttributedStringKey.font.rawValue: UIFont.boldSystemFont(ofSize: 21.0), NSAttributedStringKey.foregroundColor.rawValue:UIColor.red]    UITextField.appearance(whenContainedInInstancesOf: [CustomSearchBar.self]).defaultTextAttributes = defaultTextAttribs

Obj-C(iOS11)

    UIFont *font = [UIFont boldSystemFontOfSize:21.0];    UIColor *color = [UIColor redColor];    NSDictionary * defaultTextAttribs = @{NSFontAttributeName:font, NSForegroundColorAttributeName: color};    [UITextField appearanceWhenContainedInInstancesOfClasses:@[[CustomSearchBar class]]].defaultTextAttributes = defaultTextAttribs;