简体   繁体   中英

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?

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

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)

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)
    }
}
searchBar.searchTextField.font = UIFont(name: "Helvetica", size: 40)

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

Swift 5 中还有一种更简单的方法:

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

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM