Search code examples
swiftplaceholderuisearchcontrollerios13searchbar

How to change Searchbar's Placeholder font and size in iOS13


I read a couple of stack overflow entries to change a searchBar's placeholder text-attributes. However, in iOS13, none of them really work.

I wonder how the font, font-size and font-color of a searchBar Placeholder can be changed under iOS13 ?

Here is what I tried:

let myAttributes = [NSAttributedString.Key.font: UIFont(name: "Avenir-Heavy", size: 28) as Any]

navigationItem.searchController?.searchBar.placeholder = 
NSAttributedString(string: "placeholder text", attributes: myAttributes).string

Solution

  • Swift 5:

     if let textfield = searchBar.value(forKey: "searchField") as? UITextField {
                    let atrString = NSAttributedString(string: "Search",
                                                       attributes: [.foregroundColor : color,
                                                                    .font : UIFont.systemFont(ofSize: 10, weight: .bold)])
                    textfield.attributedPlaceholder = atrString
    
                }