Changing Search Bar placeholder text font in Swift Changing Search Bar placeholder text font in Swift xcode xcode

Changing Search Bar placeholder text font in Swift


 //SearchBar Text    let textFieldInsideUISearchBar = dashBoardSearchBar.valueForKey("searchField") as? UITextFieldtextFieldInsideUISearchBar?.textColor = UIColor.whiteColor()//SearchBar Placeholder         let textFieldInsideUISearchBarLabel = textFieldInsideUISearchBar!.valueForKey("placeholderLabel") as? UILabeltextFieldInsideUISearchBarLabel?.textColor = UIColor.whiteColor()


Set placeholder text font size:

UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 12)

set search text font size:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 12)


This is the easiest practise for changing the Font or any other similar changes in the textfield of searchBar. I have been using XCode 8.4, Swift 3.x, iOS 10.x.

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

The above code can be called directly where you make an IBOutlet of the searchBar...

@IBOutlet weak var searchBar: UISearchBar! {    didSet {        searchBar.change(textFont: GlobalConstants.Font.avenirBook14)    }}