我正在尝试从UITextView(和或UITextField)中获取所选文本范围,以便我可以编辑所选文本,或修改归属的字符串.当我进行选择时,会触发下面的方法,但该方法中的代码将返回空值.
- (void)textViewDidChangeSelection:(UITextView *)textView { UITextRange *selectedRange = [textField selectedTextRange]; NSLog(@"Start: %@ <> End: %@",selectedRange.start,selectedRange.end); }
解决方法@H_404_6@
你可以试试这个,- (void)textViewDidChangeSelection:(UITextView *)textView {
UITextRange *selectedRange = [textView selectedTextRange];
NSString *selectedText = [textView textInRange:selectedRange];
}
- (void)textViewDidChangeSelection:(UITextView *)textView { UITextRange *selectedRange = [textView selectedTextRange]; NSString *selectedText = [textView textInRange:selectedRange]; }