cocoa-touch如何在UITextview中找到所选文本以在NSString中发送文本

时间:2012-12-27 14:26:22

标签: cocoa-touch uitextview

如何在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
}

无论如何都要认识到这一点?相比?检查范围或什么?

1 个答案:

答案 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];