Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to highlight text in a textView in Swift?

I would like to highlight a part of text in a UITextView like Pages for iOS or Safari for iOS do. I search the documentation but I can't find a way to do it, maybe because I'm a beginner in iOS development and I miss some crucial info. Somebody can help me?

Screenshot of Pages for iOS:

Pages for iOS

Screenshot of Safari for iOS:

Safari for iOS

like image 951
Cue Avatar asked Jun 18 '16 09:06

Cue


1 Answers

If you only need to highlight a single, contiguous block of text you can set the selection programmatically. Your examples show multiple discontinuous parts however. For that you are going to need to use an attributed string. There is no other system-provided highlighting function. (There might be a third party library that offers this though - you'll have to search.)

like image 128
Duncan C Avatar answered Sep 20 '22 12:09

Duncan C