How can I change the font size of UISearchBar
?
Add below code where you define or setting up your UISearchBar
Swift4 :
UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.init(name: "Ubuntu-Regular", size: 16)
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.init(name: "Ubuntu-Regular", size: 16)
Objective C:
[[UILabel appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:12.0]];
The UISearchBar has a UITextField inside, but there's no property to access it. So, there is no way for doing it in a standard way.
But there is a non-stardard way to workaround it. UISearchBar inherits from UIView, you can access it's subviews using [searchBar subviews]. If you print in the console it's subviews you will see that it have these views.
UISearchBarBackground, UISearchBarTextField.
So, to change the font size you will only need to do that
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];
But, if the UISearchBar changes in the future and the textfield isn't in the position 1 anymore, your program will crash, so it's better to check through the subviews the position of the UITextField and then set the font size.
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)
I wanted to use 'Roboto-Regular' as default font for all the uisearchbars in my project. I made an uisearchbar extension.
extension UISearchBar {
func addRobotoFontToSearchBar(targetSearchBar:UISearchBar?) -> UISearchBar
{
let textFieldInsideSearchBar = targetSearchBar!.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont(name: "Roboto-Regular", size: 15.0)
//UIBarButtons font in searchbar
let uiBarButtonAttributes: NSDictionary = [NSFontAttributeName: UIFont(name: "Roboto-Regular", size: 15.0)!]
UIBarButtonItem.appearance().setTitleTextAttributes(uiBarButtonAttributes as? [String : AnyObject], forState: UIControlState.Normal)
return targetSearchBar!
}
}
Usage:
self.sampleSearchBar.addRobotoFontToSearchBar(sampleSearchBar)
iOS 13 or later:
if #available(iOS 13.0, *) {
// set your desired font size
self.searchBar.searchTextField.font = .systemFont(ofSize: 14.0)
}
If you need a convenient way to change all your application searchbars, here's a simple subclass in Swift 5:
class MySearchBar: UISearchBar {
override func layoutSubviews() {
super.layoutSubviews()
if let textFieldInsideSearchBar = value(forKey: "searchField") as? UITextField {
textFieldInsideSearchBar.font = UIFont(name: "Oswald-Light", size: 14)
}
}
}