How can I change the font size of UISearchBar
?
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;
}
}
I know this is an old thread, but since iOS 13 UISearchBar has the searchTextField property on which you can set your desired font.
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)
In swift 3 , you can achieve this by :
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont(name: "AvenirNextCondensed-Regular", size: 17.0)
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)
}
}
iOS 13+
searchBar.searchTextField.font = UIFont(name: "YOUR-FONT-NAME", size: 13)