2012-12-27 86 views
0
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text 
{ 
    NSLog (@"This is selected text in UITextView %@", selectedText); 
} 

如何打印出UITextView中的選定文本?如何在UITextview中打印出選定的文本?

回答

1
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]]; 
NSLog (@"This is selected text in UITextView %@", selectedText); 
+0

當用戶更改選定的文本時,如何才能使用實時UItextview? (無效)textViewDidChangeSelection:(UITextView *)textView { NSString * selectedText = [textView.text substringWithRange:[textView selectedRange]]; NSLog(@「This is selected text in UITextView%@」,selectedText); //當選擇文本被創建或更改時調用此方法 } – gunhi

1

爲UITextView創建出口,如yourTextViewName

(請確保它連接,以防萬一你不使用拖拽功能。)

然後在任何方法使用該語句。

_yourTextViewName.text = [_yourTextViewName.text substringWithRange:[_yourTextViewName selectedRange]]; 
+0

發佈此答案後,我在Daniel的答案中閱讀了您的評論。請解釋一下你究竟想做什麼?正如我的例子..你想自動更新'@「一些文本」'? –

0

我知道這已被回答,但我想增加更多的清晰度。我創建了幾個動態的UITextViews,並希望能夠簡單地檢測用戶點擊了哪一個。

它添加到的UITextView:

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapRecognized:)]; 
singleTap.numberOfTapsRequired = 1; 
[textView addGestureRecognizer:singleTap]; 

我用什麼方式不同:

UITextView *selectedTextView = (UITextView *)gestureRecognizer.view; 

添加到您的視圖控制器識別用戶點擊:

- (void)tapRecognized:(UIGestureRecognizer *)gestureRecognizer 
{ 
    UITextView *selectedTextView = (UITextView *)gestureRecognizer.view; 
    NSLog (@"UITextView test:%@", selectedTextView.text); 
} 
相關問題