- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
NSLog (@"This is selected text in UITextView %@", selectedText);
}
如何在UITextView
?
答案 0 :(得分:1)
NSString *selectedText = [textView.text substringWithRange:[textView selectedRange]];
NSLog (@"This is selected text in UITextView %@", selectedText);
答案 1 :(得分:1)
为UITextView创建一个插座,例如yourTextViewName
。
(确保已连接,以防您不使用拖动功能。)
然后在任何方法中使用此语句。
_yourTextViewName.text = [_yourTextViewName.text substringWithRange:[_yourTextViewName selectedRange]];
答案 2 :(得分: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);
}