How to change UISearchBar Font Size & Color?

Chhorn Soro picture Chhorn Soro · Feb 26, 2018 · Viewed 9.1k times · Source

I've google fews hours how to change my UISearchBar font size & color, but I cannot any documents related to it.

This is what I've done so far on swift 4:

 searchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: view.frame.width - (menuImage.frame.width + iconImage.frame.width + 55), height: 30))
 searchBar.placeholder = "SEARCH BAR"
 searchBar.tintColor = UIColor.gray
 searchBar.delegate = self
 searchBar.font = [UIFont fontWithName:@"Oswald" size:11];

but it gave me an error.

Could you tell me how can I change the UISearchBar font size & color?

Please help!!!

Answer

Vjardel picture Vjardel · Feb 26, 2018

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