Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grab SELECTED text on UITextView

How do I grab the SELECTED or HIGHLIGHTED text on a UITextView? I already know how to do this on UIWebView using JavaScript. Now I am trying to figure it out for the UITextView.

like image 615
Sharief Avatar asked Apr 14 '10 01:04

Sharief


2 Answers

you can do it by

NSRange range = [txtView selectedRange];
NSString *str = [txtView.text substringWithRange:range];
like image 195
Mihir Mehta Avatar answered Sep 18 '22 22:09

Mihir Mehta


Swift 3.0

In Swift, getting the selected text from a UITextView is done by first getting the selected text range (a UITextRange), and then using that range to get the actual text:

if let textRange = myTextView.selectedTextRange {
    let selectedText = myTextView.text(in: textRange)
}
like image 38
user3069232 Avatar answered Sep 17 '22 22:09

user3069232