5

I followed this : UILabel and NSLinkAttributeName: Link is not clickable and UITextView link is clickable, but when I click it, Safari doesn't open but to no avail.

I have :

uitextView.attributedText = ... some attributed string "http://google.com" ...

"Links detection", "selectable" and "user interaction enabled" are enabled. "editable" is disabled.

I also implemented UITextViewDelegate

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{

    return YES;
}

However, the link appears blue but when I click, nothing happens.

Community
  • 1
  • 1
  • Are you setting your delegate properly? Are you sure `textView:shouldInteractWithURL:inRange:` is actually being called? – JAL Jun 29 '15 at 17:41
  • Do you set `uitextView.delage = self` and your class up like this? `@interface MyView : UIViewController ` – JAL Jun 29 '15 at 18:08
  • @Lucas, have you found a solution to this issue? I am facing the same problem exactly. – Georges Dec 04 '15 at 03:33

2 Answers2

2

I had the same issue. Nothing did help.

The problem was, that i added UITextView to ScrollView and then used sizeToFit.

For example: I had view.frame (0,0,320,440). And ScrollView contentSize (0,0,320,1000) And links in view.frame works but when you scroll down - not.

Than i tried to check what view is selecting when you scroll down and tap on UITextView. I added UITapGestureRecognizers and find out that when i tap in bounds of self.view frame in bounds of UITextView it tapped on UITextView. But when i scroll down - the tap recognize as tap on ScrollView. In other words it is like frame of UITextView clips by self.view bounds.

EDIT: I found the problem:

I had View - ScrollView - ContentView - Elements hierarchy. So there was a time, when ScrollView was bigger than ContentView, so link can be tapped only in bounds of ContentView. To fix this i deleted ContentView and place all Elements to ScrollView. Hope this answer will help somebody:)

Nike Kov
  • 12,630
  • 8
  • 75
  • 122
  • This should be marked as an correct answer. Sadly, ContentView which needs to be added becouse of autolayout makes problems there. When making this, one shall also be carefull to move ContentView approprieatelly, so it will not block touches, if UITextView is behind it. – Reconquistador Dec 16 '16 at 09:34
1

Try take this approach:

NSString *clickMe = [NSString stringWithFormat:@"%@", word];
    NSMutableAttributedString * str2 = [[NSMutableAttributedString alloc] initWithString:word];
    [str2 addAttribute: NSLinkAttributeName value:[NSURL URLWithString:[NSString stringWithFormat:@"https://twitter.com/%@",linkWord]] range:NSMakeRange(0, clickMe.length)];

    // replace with link
    [YourAttributedString replaceCharactersInRange:wordRange withAttributedString:str2];
Alessign
  • 768
  • 9
  • 17