How can I change the font size and font style of UISearchBar in iOS 7?
UITextField *textField = [[searchBar subviews] objectAtIndex:1];
[textField setFont:[U
appearanceWhenContainedIn:
is deprecated in iOS9, use the following:
[[UITextField appearanceWhenContainedInInstancesOfClasses:@[[UISearchBar class]]] setDefaultTextAttributes:@{ NSFontAttributeName: [UIFont fontWithName:@"FontName" size:14]}];
For me, this worked using a recursive function to find the actual textfield.
extension UIView {
func recursive_applyTheme_Search(
#dynamicTextStyle: NSString,
bgColor: UIColor,
cursorColor: UIColor,
textColor: UIColor,
placeholderTextColor: UIColor,
borderColor: UIColor,
borderWidth: CGFloat,
cornerRadius: CGFloat) {
for subview in self.subviews
{
if subview is UITextField {
(subview as! UITextField).applyThemeForSearchBar(
dynamicTextStyle: dynamicTextStyle,
bgColor: bgColor,
cursorColor: cursorColor,
textColor: textColor,
placeholderTextColor: placeholderTextColor,
borderColor: borderColor,
borderWidth: borderWidth,
cornerRadius: cornerRadius)
}
else { subview.recursive_applyTheme_Search(
dynamicTextStyle: dynamicTextStyle,
bgColor: bgColor,
cursorColor: cursorColor,
textColor: textColor,
placeholderTextColor: placeholderTextColor,
borderColor: borderColor,
borderWidth: borderWidth,
cornerRadius: cornerRadius) }
}
}
}
iOS 10 Swift 3:
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSFontAttributeName:UIFont(name: "Avenir-Heavy", size: 22)!]