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

如何打印出选定的文本UITextView

4

3 回答 3

1
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
NSLog (@"This is selected text in UITextView %@", selectedText);
于 2012-12-27T12:41:14.477 回答
1

为 UITextView 创建一个出口,比如说yourTextViewName

(确保它已连接,以防万一您不使用拖动功能。)

然后在任何方法中使用这个语句。

_yourTextViewName.text = [_yourTextViewName.text substringWithRange:[_yourTextViewName selectedRange]];
于 2012-12-27T13:29:07.820 回答
0

我知道这已得到解答,但我想更清楚地说明。我已经创建了几个动态 UITextViews,并希望能够简单地检测用户点击了哪一个。

将此添加到 UITextView:

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

我使用的不同:

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

将此添加到您的 ViewController 以识别用户点击:

- (void)tapRecognized:(UIGestureRecognizer *)gestureRecognizer 
{
    UITextView *selectedTextView = (UITextView *)gestureRecognizer.view;
    NSLog (@"UITextView test:%@", selectedTextView.text);
}
于 2014-02-22T17:29:35.667 回答