Reputation:
I was wondering is there any way to get the point of the selected text in UITextView ?
Thanks!
Upvotes: 1
Views: 3478
Reputation: 33423
Yes, first get the active selection like this: myTextView.selectedRange
and then pass it to this method [myTextView firstRectForRange:range]
This will give you the first bounding rectangle (which is the entire selection if it is one line, and the area of the selection on the first line if it is multi-line) as a CGRect
.
Upvotes: 1
Reputation: 73688
For iOS5 and above : Now UITextField
and UITextView
conform to UITextInput
protocol so it is possible :)
Selecting the last 5 characters before the caret would be like this:
//Get current selected range , this example assumes is an insertion point or empty selection
UITextRange *selectedRange = [textField selectedTextRange];
NSLog("Start: %d <> End: %d", selectedRange.start, selectedRange.end);
//Calculate the new position, - for left and + for right
UITextPosition *newPosition = [textField positionFromPosition:selectedRange.start offset:-5];
//Construct a new range using the object that adopts the UITextInput, our textfield
UITextRange *newRange = [textField textRangeFromPosition:newPosition toPosition:selectedRange.start];
Upvotes: 2