0

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

有没有办法承认这一点?相比?检查范围还是什么?

4

1 回答 1

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];
于 2012-12-27T16:31:06.907 回答