You need something like this:
- (NSRange) selectedRangeInTextView:(UITextView*)textView
{
UITextPosition* beginning = textView.beginningOfDocument;
UITextRange* selectedRange = textView.selectedTextRange;
UITextPosition* selectionStart = selectedRange.start;
UITextPosition* selectionEnd = selectedRange.end;
const NSInteger location = [textView offsetFromPosition:beginning toPosition:selectionStart];
const NSInteger length = [textView offsetFromPosition:selectionStart toPosition:selectionEnd];
return NSMakeRange(location, length);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…