2

I'm trying to set the placeholder text colour of a UISearchField in iOS 13 (beta).

My code worked in iOS 12 and before. I can still change the UISearchField background colour, the colour of the search icon, and so on...

Here's what I'm trying:

searchTextField.attributedPlaceholder = NSAttributedString(
  string: "My placeholder text",
  attributes: [
    NSAttributedString.Key.foregroundColor: UIColor.red
  ]
);

I expect the placeholder text to now be red, but it remains grey.

Swift Dev Journal
  • 19,282
  • 4
  • 56
  • 66
SuddenMoustache
  • 883
  • 7
  • 17
  • By the way you code works fine. Seems that your problem is in another point. probably you don't call this code – Dmitry Sep 03 '19 at 10:01
  • 1
    @dmitry: none of the suggestions in that thread (almost all of which are identical to the above) work in iOS 13. The code is being called and I'm using a minimal example. If it's working for you, perhaps you're not using iOS 13. – SuddenMoustache Sep 03 '19 at 10:14
  • probably you are right. I used iOS 12.2 to test – Dmitry Sep 03 '19 at 10:22
  • 1
    Well, as the title and the post itself say, this issue affects iOS 13 only. – SuddenMoustache Sep 04 '19 at 12:24
  • @Matt Any update on this question? I didn't find any answer either, this is triggering. How hard it is to just change a text color on the placeHolder. This is because of Dark/Light mode, I don't want the system to override it. – Funnycuni Oct 11 '19 at 09:26
  • @Funnycuni I'm afraid I didn't find an answer. We had to change our app's UX to accommodate the colour we can't change... – SuddenMoustache Oct 14 '19 at 09:29
  • 1
    **Check this for Xcode11 and iOS 13 :** https://stackoverflow.com/a/58375484/7851805 – Nick Oct 14 '19 at 12:32
  • @Nick that answer uses setValue(_, forKey:), which is a private API the use of which can get one banned from the app store. – SuddenMoustache Oct 21 '19 at 08:59

1 Answers1

0

you can use NSMutableAttributeString as below,

  let attrString = NSMutableAttributedString(string: "your placeHolder")

    attrString.addAttributes([NSAttributedString.Key.foregroundColor: UIColor.red, range: NSRange(location: 0, length: attrString.length))

searchTextField.attributedPlaceholder = attrString
Hanryang
  • 231
  • 2
  • 12
  • 1
    This doesn't make a difference for me. And should we expect it to?-- the mutability of the attributed string doesn't seem to matter here. – SuddenMoustache Sep 03 '19 at 10:15