Changing Search Bar placeholder text font in Swift

user3746428 picture user3746428 · Oct 18, 2014 · Viewed 24.8k times · Source

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?

Answer

A.G picture A.G · Dec 28, 2015
 //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()