2012-12-27 77 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]; 
相關問題