- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog (@"This is selected text in UITextView %@", selectedText);
}
如何打印出选定的文本UITextView
?
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog (@"This is selected text in UITextView %@", selectedText);
}
如何打印出选定的文本UITextView
?
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
NSLog (@"This is selected text in UITextView %@", selectedText);
为 UITextView 创建一个出口,比如说yourTextViewName
。
(确保它已连接,以防万一您不使用拖动功能。)
然后在任何方法中使用这个语句。
_yourTextViewName.text = [_yourTextViewName.text substringWithRange:[_yourTextViewName selectedRange]];
我知道这已得到解答,但我想更清楚地说明。我已经创建了几个动态 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);
}