'Changing Search Bar placeholder text font in Swift

I am trying to change the font of the placeholder text in the search bar within my Search Display Controller. I was looking at some examples and I tried to implement them but as they are in Objective-C, I wasn't able to find any that I could get to work.

For example, I tried this one:

UITextField *textField = [[searchBar subviews] objectAtIndex:1]; 
[textField setFont:[UIFont fontWithName:@"Helvetica" size:40]];

But I was unable to get past var textField: UITextField = UISearchBar

Any ideas?



Solution 1:[1]

 //SearchBar Text
    let textFieldInsideUISearchBar = dashBoardSearchBar.valueForKey("searchField") as? UITextField
textFieldInsideUISearchBar?.textColor = UIColor.whiteColor()

//SearchBar Placeholder    
     let textFieldInsideUISearchBarLabel = textFieldInsideUISearchBar!.valueForKey("placeholderLabel") as? UILabel
textFieldInsideUISearchBarLabel?.textColor = UIColor.whiteColor()

Solution 2:[2]

Set placeholder text font size:

UILabel.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 12)

set search text font size:

UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).font = UIFont.systemFont(ofSize: 12)

Solution 3:[3]

This is the easiest practise for changing the Font or any other similar changes in the textfield of searchBar. I have been using XCode 8.4, Swift 3.x, iOS 10.x.

extension UISearchBar {

func change(textFont : UIFont?) {

    for view : UIView in (self.subviews[0]).subviews {

        if let textField = view as? UITextField {
            textField.font = textFont
        }
    }
} }

The above code can be called directly where you make an IBOutlet of the searchBar...

@IBOutlet weak var searchBar: UISearchBar! {
    didSet {
        searchBar.change(textFont: GlobalConstants.Font.avenirBook14)
    }
}

Solution 4:[4]

searchBar.searchTextField.font = UIFont(name: "Helvetica", size: 40)

Solution 5:[5]

There is even an easier way in Swift 5:

searchBar[keyPath: \.searchTextField].font = UIFont(...)

Solution 6:[6]

In iOS 8 ,try this

for subView in searchBar.subviews  {
  for subsubView in subView.subviews  {
      if let textField = subsubView as? UITextField {
        textField.attributedPlaceholder =  NSAttributedString(string:NSLocalizedString("Search", comment:""),
          attributes:[NSForegroundColorAttributeName: UIColor.orangeColor()])
      }
  }
}

Solution 7:[7]

Swift 3 version of @Alvin's answer

let textFieldInsideUISearchBar = searchBar.value(forKey: "searchField") as? UITextField
    let placeholderLabel       = textFieldInsideUISearchBar?.value(forKey: "placeholderLabel") as? UILabel
    placeholderLabel?.font     = UIFont.systemFont(ofSize: 12.0)

Solution 8:[8]

This works perfectly for ios7 -> ios9 using swift 2:

if #available(iOS 9.0, *) {
    UITextField.appearanceWhenContainedInInstancesOfClasses([UISearchBar.self]).font = UI.getInstance.tinyFont
} else {
    func checkSubview(view:UIView)
    for subView in view.subviews {
        if subView is UITextField {
            let textField = subView as! UITextField
            textField.font = UI.getInstance.tinyFont
        } else {
            checkSubview(subView)
        }

    }
    checkSubview(view)
}

Just replace UI.getInstance.tinyFont by whichever font you want.

Solution 9:[9]

let searchBar = UISearchBar()

guard let font = UIFont(name: "Helvetica", size: 40.0) else { return }

let attributedString = NSAttributedString(
    string: "Search...",
    attributes: [
        .font: font
    ]
)

searchBar.searchTextField.attributedPlaceholder = attributedString

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 SoundShock
Solution 2 ldehai
Solution 3 Mr. Bean
Solution 4 Sourabh Kumbhar
Solution 5 ramzesenok
Solution 6 Arshad
Solution 7 Ajay Kumar
Solution 8 ThomasE
Solution 9 Dharman