Change the font size and font style of UISearchBar iOS 7
15,769
Solution 1
Try this, It's Working Fine for iOS 5.0 and up: (iOS 7 also)
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setFont:[UIFont fontWithName:@"Helvetica" size:20]];
}
For iOS 8
- (void)viewDidLoad
{
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20],
}];
}
Solution 2
The accepted answer did not work for me on iOS 7.1. I had to change it to this:
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{
NSFontAttributeName: [UIFont fontWithName:@"Avenir-Heavy" size:20.],
}];
Solution 3
iOS 10 Swift 3:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]
Solution 4
appearanceWhenContainedIn:
is deprecated in iOS9, use the following:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];
Solution 5
In Swift 5
code of @Piotr Tomasik still working in swift 5 after bit changes
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedString.Key.font:UIFont(name: "poppins", size: 13)!]
Related videos on Youtube
Comments
-
SWT about 2 years
How can I change the font size and font style of UISearchBar in iOS 7?
UITextField *textField = [[searchBar subviews] objectAtIndex:1]; [textField setFont:[UIFont fontWithName:@"Helvetica" size:20]];
Working in iOS 6 but it's getting crash in iOS 7
-
rmaddy over 10 yearsWhat is the error? And never dig into the private subview structure of a standard UI component. That stuff changes and makes your code break. Use provided APIs to do this stuff.
-
o15a3d4l11s2 over 10 yearsPlease check this: stackoverflow.com/questions/19048766/… I think what you try to achieve is the same.
-
Bhavin over 10 years@rmaddy is correct and detailed explanation of safe approach is given in my Answer.
-
-
Manann Sseth over 10 yearsWorks like a charm..!
-
rickerbh over 10 yearsThe second approach here doesn't appear to be that safe. You're still relying on the subview structure, as you're assuming that a UISearchBar has a UITextField as as direct subview. Just because you're avoiding an index doesn't mean you're avoiding the private subview structure.
-
Bhavin over 10 years@rickerbh: Yeah, true. Actually, even I believe that OP should use the first approach that I mentioned in my Answer.
-
Ganesh almost 10 yearsI have small query on this. I have implemented the same and it is working.But if I navigate to some view controller and back to the same screen the font is changing to default.Any Idea?
-
Mike almost 10 yearsSame thing here on iOS 8. Works fine the first time. But when I navigate to another screen and back, the font is back to the system default.
-
Mike almost 10 yearsThis way overcomes the issue [tried on iOS 8.0] mentioned in Ganesh's comment (that by only using the setFont and not setDefaultTextAttribute the text is only changed on the first view and not after navigating from and back to the screen in questions).
-
Harshit Gupta over 9 yearsthis answer is better.
-
Mike Gledhill over 9 yearsYup, this was the only method which would work each time for me, on iOS 8.1.
-
Andrei Popa about 9 yearsAlso, most of WWDC videos recommend that font should conform to Dynamic Type, use let font = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
-
jeet.chanchawat over 8 yearsdeveloper.apple.com/library/ios/documentation/UIKit/Reference/… UISearchBar has no hierarchy from textfield.
-
Adam Kaplan over 8 years@jeet.chanchawat – that does not matter. The appearance is defined for UITextFields that are subviews of UISearchBar, and there is one in there.
-
Rishi over 6 yearsWorks as expedited in iOS11(Swift 4). Especially .rawValue is what I was missing.
-
Sathe_Nagaraja over 6 years'appearanceWhenContainedIn:' is deprecated: first deprecated in iOS 9.0 - Use +appearanceWhenContainedInInstancesOfClasses: instead [[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"Helvetica" size:20] }];
-
Erick Maynard almost 6 yearsType 'NSAttributedStringKey' (aka 'NSString') has no member 'font'
-
Konstantin Berkov almost 6 years
let appearence = UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self])
This doesn't work:appearence.font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.heavy)
But this works fine:appearence.defaultTextAttributes = [NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.heavy)]