因为UITextView和UITextField都有UITextInput协议,UITextInput方法里可获取到UITextPosition *
修改方法代码如下
- (void)selectTextForInput:(UITextView *)input atRange:(NSRange)range {
UITextPosition *start = [input positionFromPosition:[input beginningOfDocument]
offset:range.location];
UITextPosition *end = [input positionFromPosition:start
offset:range.length];
[input setSelectedTextRange:[input textRangeFromPosition:start toPosition:end]];
}
入参,为要修改的TextView 或UITextField ,然后要设置的位置,选中的长度
参考 点击打开链接