JLO JLO - 4 months ago 102x
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?


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()