5
votes

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.

2
Are you setting your delegate properly? Are you sure textView:shouldInteractWithURL:inRange: is actually being called? - JAL
Do you set uitextView.delage = self and your class up like this? @interface MyView : UIViewController <UITextViewDelegate> - JAL
@Lucas, have you found a solution to this issue? I am facing the same problem exactly. - Georges

2 Answers

2
votes

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:)

1
votes

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];