user3746428
user3746428

Reputation: 11175

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?

Upvotes: 25

Views: 31137

Answers (9)

Booharin
Booharin

Reputation: 789

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

Upvotes: 0

Sourabh Kumbhar
Sourabh Kumbhar

Reputation: 1054

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

Upvotes: 16

ramzesenok
ramzesenok

Reputation: 6931

There is even an easier way in Swift 5:

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

Upvotes: 9

ldehai
ldehai

Reputation: 604

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)

Upvotes: 22

Ajay Kumar
Ajay Kumar

Reputation: 1827

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)

Upvotes: 7

Mr. Bean
Mr. Bean

Reputation: 4281

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

Upvotes: 15

Alvin George
Alvin George

Reputation: 14296

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

Upvotes: 34

ThomasE
ThomasE

Reputation: 59

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.

Upvotes: 3

Arshad
Arshad

Reputation: 906

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

Upvotes: 8

Related Questions