Daniel
Daniel

Reputation: 21

How can I highlight a portion of the text of a UITextView?

What I would like to do is, if my detailed view gets called I would like to highlight just a part of the text of the UITextView element.

Let's say the text within the UITextView is: blatextbla. Then I would like to highlight dynamically only text or late or whatever comes to the user's mind.

How can I do this?

Upvotes: 2

Views: 4573

Answers (3)

KingofBliss
KingofBliss

Reputation: 15115

You can't highlight a text in a UITextView.

Instead you can go with NSAttributedString.

Upvotes: 3

wkw
wkw

Reputation: 3863

You'll need to get the ranges within your text where the text to be highlighted starts and ends then use:

NSRange selection = [yourNSString rangeOfString:@"late"];
if( selection.location != NSNotFound ){
   myTextView.selectedRange =  selection;
}

I've never tried to select multiple ranges within a UITextView, so not sure if that's possible. I doubt it's possible...

Upvotes: 1

UMER
UMER

Reputation: 41

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text

This is a built-in function through which you can find and replace text in a TextView.

Upvotes: 0

Related Questions