Change the font size of UISearchBar
IosObjective CUisearchbarUifontIos Problem Overview
How can I change the font size of UISearchBar
?
Ios Solutions
Solution 1 - Ios
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.
Solution 2 - Ios
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)
Solution 3 - Ios
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;
Solution 4 - Ios
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.
Solution 5 - Ios
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]];
Solution 6 - Ios
In swift 2.0 for a search bar created programatically you could do this:
override func layoutSubviews() {
super.layoutSubviews()
let textFieldInsideSearchBar = self.searchBar.valueForKey("searchField") as! UITextField
textFieldInsideSearchBar.font = UIFont.systemFontOfSize(20)
}
In this case I put the code in a UIView subclass but it will work in other places too (i.e. viewWillAppear from the UIViewController)
Solution 7 - Ios
In swift 3 , you can achieve this by :
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont(name: "AvenirNextCondensed-Regular", size: 17.0)
Solution 8 - Ios
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"
}
Solution 9 - Ios
UITextField
appearance does not work for UISearchBar
s 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];
}
}
Solution 10 - Ios
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)
Solution 11 - Ios
iOS 13+
searchBar.searchTextField.font = UIFont(name: "YOUR-FONT-NAME", size: 13)
Solution 12 - Ios
Rewrote Eugene's recursive solution in swift - changed the function name to more accurately describe function. For my case, I needed to change the font size. This worked for me.
func findAndSetTextField(view: UIView) {
if (view.subviews.count == 0){
return
}
for var i = 0; i < view.subviews.count; i++ {
var subview : UIView = view.subviews[i] as UIView
if (subview.isKindOfClass(UITextField)){
var searchField : UITextField = subview as UITextField
searchField.font = UIFont(name: "Helvetica", size: 19.0)!
}
findAndSetTextField(subview)
}
}
Solution 13 - Ios
Try finding the search bar by its key:
UITextField *searchField = [self.searchDisplayController.searchBar valueForKey:@"_searchField"];
searchField.font = [[UIFont fontWithName:@"Oswald" size:11];
Solution 14 - Ios
the full code should be below:
for (UIView *v in (SYSTEM_VERSION_LESS_THAN(@"7.0")?searchBar.subviews:[[searchBar.subviews objectAtIndex:0] subviews])) {
if([v isKindOfClass:[UITextField class]]) {
UITextField *textField = (UITextField *)v;
[textField setFont:fREGULAR(@"15.0")];
return;
}
}
Solution 15 - Ios
I know this is an old thread, but since iOS 13 UISearchBar has the searchTextField property on which you can set your desired font.
Solution 16 - Ios
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)
}
}
}
Solution 17 - Ios
iOS 13 or later:
if #available(iOS 13.0, *) {
// set your desired font size
self.searchBar.searchTextField.font = .systemFont(ofSize: 14.0)
}
Solution 18 - Ios
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]];
Solution 19 - Ios
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