2
votes

I am developing an iOS 8.0+ app in Swift 2.0 that uses UITextView to display Strings of various lengths (ranging from about 100 characters to about 60,000 characters).

When the UITextView appears with its .text value set to a given String, I would like the UITextView to automatically highlight (in yellow) all of the occurrences of a particular substring contained in an optional variable that is prepopulated in the view controller. (If the optional variable is not set, then nothing is highlighted.)

How can I: 1) identify and select all of the occurrences of a particular substring in a String, and 2) highlight each occurrence in yellow in a UITextView?

Thank you!

1

1 Answers

2
votes

UITextView doesn't support multiple selections. Your only real option is to create an NSAttributedString and apply a colored background to all of the matching substrings. Then set the text view's attributedText property.