Change UISearchBar textColor not working
Asked Answered
X

7

5

I am using google places API for autoComplete widget. I am showing the full screen control in ios 9+ using swift. I am adding a full control as given in the docs.

I have added the code as shown in the docs. Now I want to change the searchBar text color to whiteColor.

So I tried this

UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).textColor = UIColor.whiteColor()

But I am not getting the desired behaviour. Below is the screenshot

**enter image description here**

This has been given in Docs

https://developers.google.com/places/ios-api/autocomplete#use_a_table_data_source

But this is not working. I need help with regarding to this.

Xena answered 11/6, 2016 at 11:18 Comment(2)
explain the desired output and the error you are gettingViscose
Its always better to elaborate question a bit and attach snapshot if possible otherwise difficult to get exact solution.Prefix
A
10

You need to create an extension like follwoing:

public extension UISearchBar {

    public func setNewcolor(color: UIColor) {
        let clrChange = subviews.flatMap { $0.subviews }
        guard let sc = (clrChange.filter { $0 is UITextField }).first as? UITextField else { return }
        sc.textColor = color
    }
}

And change color using following code:

 controller.searchBar.setNewcolor(UIColor.redColor())

Output is :

enter image description here

Update:

For the change color of searchBar text for the GMSAutocompleteViewController you need to do following code:

let searchBarTextAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.redColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes

That change the text out put like following image:

enter image description here

And if you wish to change placeholder text and it's color for searchBar. You need to do following code:

let placeholderAttributes: [String : AnyObject] = [NSForegroundColorAttributeName: UIColor.whiteColor(), NSFontAttributeName: UIFont.systemFontOfSize(UIFont.systemFontSize())]

let attributedPlaceholder: NSAttributedString = NSAttributedString(string: "Find a place", attributes: placeholderAttributes)
UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).attributedPlaceholder = attributedPlaceholder

It will be show like:

enter image description here

Amp answered 15/6, 2016 at 10:51 Comment(0)
S
6

Use Bellow code for Swift 3

if #available(iOS 9.0, *) {
  UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSForegroundColorAttributeName: UIColor.green]
  } else {
  // Fallback on earlier versions
}

Hope this helps.

Subdued answered 12/3, 2017 at 10:30 Comment(0)
G
3

For swift 4 ios 12

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes
            .updateValue(UIColor.white, forKey: NSAttributedStringKey.foregroundColor.rawValue)
Giverin answered 19/9, 2018 at 21:56 Comment(0)
H
2

You can use UIAppearance protocol to get the appearance proxy for a class which is available in iOS 5.0 and later.

There are actually two ways to customize appearance for objects and to get the appearance proxy for the class.

  • To customize the appearance of all instances of a class, use appearance.
  • To customize the appearances for instances of a class when contained within an instance of a container class, or instances in a hierarchy, use appearanceWhenContainedIn.

You can apply this sample code:

[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setDefaultTextAttributes:@{NSForegroundColorAttributeName:[UIColor redColor]}];

You can also try another option given in this SO post - UISearchBar text color change in iOS 7.

I hope that covers your issue. Happy coding!

Hornwort answered 14/6, 2016 at 18:10 Comment(1)
It doesnt cover my issue. I am using swift and in swift we cannot use defaulttext attributes.Xena
D
1

In swift 5:

let searchBarTextAttributes: [NSAttributedString.Key : AnyObject] = [NSAttributedString.Key(rawValue: NSAttributedString.Key.foregroundColor.rawValue): UIColor.red, NSAttributedString.Key(rawValue: NSAttributedString.Key.font.rawValue): UIFont.systemFont(ofSize: 14.0)]

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = searchBarTextAttributes
Delanty answered 5/5, 2019 at 15:24 Comment(0)
B
1

This is actually very easy, just access the textField inside that searchBar as so:

searchBar.searchTextField.defaultTextAttributes = [NSAttributedString.Key.foregroundColor: UIColor.red]
Blindfold answered 20/9, 2020 at 19:9 Comment(0)
I
0

Lastest on swift 5

searchBar.searchTextField.textColor = .white
Illumine answered 3/8, 2022 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.