JLO JLO - 1 year ago 304
Swift Question

Changing the font style for UISearchBar cancel button

I am trying to change the font of the UISearchBar cancel button to 'OpenSans' but I am unable to access any properties. I can only change the tint color of UISearchBar, which changes the color of the search bar cancel button text & search bar UITextField cursor color:

searchBar.tintColor = UIColor(red: 187.0/255.0, green: 187.0/255.0, blue: 187.0/255.0, alpha: 1.0)

Is there a way to accomplish this?

Answer Source

Try something like this it worked for me.

 UIBarButtonItem.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont(name: "OpenSans", size: 15)!], forState: .Normal)

If you want to change the color as well add this to your array of attributes.

 NSForegroundColorAttributeName : UIColor.whiteColor()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download