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 に答える