简体   繁体   中英

UITextView detect link with editable property to true

I have searched a lot for how to detect link in UITextView with editable property set to true , but didn't find any solution. All solutions suggest to set editable to NO , but by requirement I can't set editable to NO .

Unfortunately you cannot have an editable UITextView with clickable links.

But you can try this code it might work. I get this from one tutorial: http://www.ama-dev.com/editable-uitextview-with-link-detection/

UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editTextRecognizerTabbed:)]; 
recognizer.delegate = self;
recognizer.numberOfTapsRequired = 1;
[self.textViewNotes addGestureRecognizer:recognizer];

- (void) editTextRecognizerTabbed:(UITapGestureRecognizer *) aRecognizer; 
{
    self.textViewNotes.dataDetectorTypes = UIDataDetectorTypeNone; 
    self.textViewNotes.editable = YES;
    [self.textViewNotes becomeFirstResponder];
}

- (void)textViewDidEndEditing:(UITextView *)textView;
{ 
    self.textViewNotes.editable = NO;
    self.textViewNotes.dataDetectorTypes = UIDataDetectorTypeAll;
}

I've found this blog . From the example: to make a UITextView editable with clickable links you should setup a gestureRecognizer and make the UITextView editable on tap. The code is:

- (void)configureTextView {
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self    action:@selector(textViewTapped:)];
    recognizer.numberOfTapsRequired = 1;
    textView.editable = NO;
    textView.dataDetectorTypes = UIDataDetectorTypeLink;
    [textView addGestureRecognizer:recognizer];
}

// Notification from the recogniser that the UITextView was tapped
- (void)textViewTapped:(UIGestureRecognizer *)recognizer {
    UITextView *textView = (UITextView *)recognizer.view;
    textView.editable = YES;
    [textView becomeFirstResponder];
}

// UITextViewDelegate method
- (void)textViewDidEndEditing:(UITextView *)textView {
    textView.editable = NO;
}

您可以为该UITextField使用UITapGestureRecognizer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM