8

How can I programmatically change the color of the prompt on mac catalyst for an UITextField?

The prompt exist but with the same color as UITextField.

The prompt is showing the right color on iOS.

I unsuccessfully tried .tintColor

Code + iOS and Mac Catalyst result

Fattie
  • 27,874
  • 70
  • 431
  • 719

1 Answers1

23

After a lot of searching around, I think I've found a workaround for this. You can use the key-value coding paradigms to get at the insertionPointColor property (which is what you ultimately need to set). Here is an example of setting the caret color to red. Be careful to only do this when targeting Mac Catalyst, as using tintColor on iOS is proper.

#if targetEnvironment(macCatalyst)
let textInputTraits = myTextView.value(forKey: "textInputTraits") as? NSObject
textInputTraits?.setValue(UIColor.red, forKey: "insertionPointColor")
#endif

As of 2022 this works perfectly for both UITextField and UITextView on Mac Catalyst builds running on a Mac.

Fattie
  • 27,874
  • 70
  • 431
  • 719
JacobJ
  • 3,677
  • 3
  • 28
  • 32