如何在NSString中的UITextview中找到所选文本
//Declare
NSString* myText = @"This is the first row and This is the second row";
myUITextview.text = myText;
// we have the same results in here "This is the" and "row"
//Then check it when text in UITextview is selected
- (void)textViewDidChangeSelection:(UITextView *)textView
{
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
//selectedText "This is" from the second not from the first
}
无论如何都要认识到这一点?相比?检查范围或什么?
答案 0 :(得分:0)
为了在UITextView中的所选文本之前和之后添加文本,你应该这样做:
NSString *fullText = textView.text;
NSRange selectedRange = [textView selectedRange];
NSString *selectedText = [fullText substringWithRange:selectedRange];
NSString *addBeforeSel = @"abc";
NSString *addAfterSel = @"def";
NSString *replace = [NSString stringWithFormat:@"%@%@%@", addBeforeSel, selectedText, addAfterSel];
NSString *result = [fullText stringByReplacingCharactersInRange:selectedRange withString:replace];