2012-12-27 24 views
0

中的文本我怎么能找到的NSString可可触摸我怎样才能找到的UITextView所选文本的NSString

//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 
} 

中在UITextView中选定的文本反正是有认识的?比较?检查范围或东西?

+0

认清究竟是什么?你的问题不是很清楚。你有特定的NSRange,你有全文和选定的文本,你错过了什么? – Ismael

+0

我的意思是,从UITextview中获取精确选定的文本,并检查位于字符串中的选定文本的位置。我需要在字符串中选择的文本之前和之后放置一些东西。然后用新的输入文本更新UITextview。 – gunhi

回答

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]; 
相关问题