mobs_boss
mobs_boss

Reputation: 852

GMSAutocompleteViewController iOS, How can I get different color for text in searchControllers?

I'm implementing the default GMSAutocompleteViewController for my app. But couldn't find any documentation or guide to change the color of text in the searchController's textField to white. right now it seems black

enter image description here

I want it be white so the text is more visible.

UPDATE : I'm using following code to change to change the 'Cancel' button color to white as well but it's the same.

let autocompleteController = GMSAutocompleteViewController()
autocompleteController.tintColor = .white

Upvotes: 4

Views: 1667

Answers (1)

mobs_boss
mobs_boss

Reputation: 852

I used this code :

Swift 2: as mentioned in the comments by @Ercell0

GMSAutocompleteViewController iOS, how to change the text color in the searchBar

Swift 4.0:

let searchBarTextAttributes: [String : AnyObject] = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.white, NSAttributedStringKey.font.rawValue: UIFont.systemFont(ofSize: UIFont.systemFontSize)]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

Swift 4.2. & above:

let autocompleteController = GMSAutocompleteViewController()
let searchBarTextAttributes: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.white, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: UIFont.systemFontSize)]
UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

Upvotes: 4

Related Questions