Colored substring in UITextField

2019-08-27 16:13发布

问题:

I would like to change the color only of particular substrings (keywords) while the user is typing in a UITextField.

In the documentation I saw the property textColor but it does change the color of the entire text string, while I would like to highlight only some keywords.

Is it possible?

回答1:

The short answer is no.

It's possible if you create your own textfield from scratch using NSAttributedStrings and CATextLayers or Core Text, but this is an incredibly complex and difficult problem.



回答2:

If you are mirroring what the user types in a preview box of some sort it is possible to create multiple labels or text fields that stay next to each other and have different colors or fonts or whatever. Otherwise, I'm afraid Nick's answer is accurate - within a single textField it ain't happening.

Good luck,

Damien