Change the Font Size of Uisearchbar

Change the font size of UISearchBar

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.

How to change UISearchBar Font Size & Color?

To change text and placeholder searchbar:

// SearchBar text
let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.red
textFieldInsideUISearchBar?.font = textFieldInsideUISearchBar?.font?.withSize(12)

// SearchBar placeholder
let labelInsideUISearchBar = textFieldInsideUISearchBar!.value(forKey: "placeholderLabel") as? UILabel
labelInsideUISearchBar?.textColor = UIColor.red

How to change Searchbar's Placeholder font and size in iOS13

Swift 5:

 if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
let atrString = NSAttributedString(string: "Search",
attributes: [.foregroundColor : color,
.font : UIFont.systemFont(ofSize: 10, weight: .bold)])
textfield.attributedPlaceholder = atrString

}

Change the font size and font style of UISearchBar iOS 7

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],
}];

}

How to increase a scope title and search bar's font size in Xcode?

setScopeBarButtonTitleTextAttributes:forState: sets the text attributes for the search bar’ button’s title string for a given state.

 let font = UIFont.systemFont(ofSize: 24)   
self.searchBar.setScopeBarButtonTitleTextAttributes([NSAttributedString.Key.font : font], for: .normal)

Answer for your question in comments.

Update font size of UITextField in searchBar

Extract the UITextField object from searchBar by iterating over subViews, and then update font of it.

extension UISearchBar {
func update(font : UIFont?) {
for view in (self.subviews[0]).subviews {
if let textField = view as? UITextField {
textField.font = font
}
}
}
}

update font use this method when initialize searchBar object

@IBOutlet weak var searchBar:UISearchBar!{
didSet {
searchBar.update(font: UIFont.systemFont(ofSize: 30))
}
}


Related Topics



Leave a reply



Submit