11

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.

Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
Raj Aggrawal
  • 761
  • 1
  • 6
  • 21

3 Answers3

11

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;
}
Pranav
  • 701
  • 4
  • 18
  • 2
    Yes , I was just going to suggest this way :) , upvoted. – Bhumit Mehta Jul 21 '16 at 06:54
  • 1
    @pranv signo, , My requirement is that when I tap on link in textview it should open in browser and when tap on textview elsewhere it should become editable. You can refer an apple's app "Note" – Raj Aggrawal Jul 21 '16 at 07:15
  • 2
    you can use this - (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange to manage tap on textview – Pranav Jul 21 '16 at 07:26
  • 1
    @PranavGupte I also use the same delegate method. It work little bit fine. I still see that some time textview become editable even tap on link. and some time Link is open. One think I notice that when I tap on link for more the 1 sec then it open the link otherwise textview become editable. And Thanks a lot for your valuable answer.. and your effort rewarded. upvoted. – Raj Aggrawal Jul 21 '16 at 07:33
  • 1
    @RajAggrawal try to call resignfirstresponder in delegate method or endEditing that should work !! you r welcome :) If you find 99.99% accurate solution using this then let me know I will edit my answer that will help others :) – Pranav Jul 21 '16 at 07:42
3

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;
}
LS_
  • 6,763
  • 9
  • 52
  • 88
  • I also tried the same and working fine but one issue still there ie. When I tap on link it not opening the link textview become responder. According to my requirement when I tap on link , it should be open in browser and when I tap in textview except lnk position it should become editable – Raj Aggrawal Jul 21 '16 at 07:08
  • @RajAggrawal with this solution the link is clickable until the textView is not being edited. you need the link clickable even if the textView is being edited? – LS_ Jul 21 '16 at 07:28
0

You can use UITapGestureRecognizer for that UITextField .