Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the font style for UISearchBar cancel button

Tags:

swift

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?

like image 415
butter_baby Avatar asked Dec 14 '22 05:12

butter_baby


1 Answers

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

Note: Made available in iOS 9

like image 98
Micah Wilson Avatar answered Feb 12 '23 03:02

Micah Wilson