Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change prompt color for UITextField on mac catalyst

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

like image 856
Christophe Vichery Avatar asked Dec 04 '22 17:12

Christophe Vichery


1 Answers

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.

like image 181
JacobJ Avatar answered Dec 27 '22 19:12

JacobJ