UILabel and NSLinkAttributeName: Link is not clickable

I can answer my own question now:
I am using UITextView instead of UILabel now. I have formatted the UITextView to look and behave like my labels and added:

UITextView *textView = [[UITextView alloc] init];
textView.scrollEnabled = NO;
textView.editable = NO;
textView.textContainer.lineFragmentPadding = 0;
textView.textContainerInset = UIEdgeInsetsMake(0, 0, 0, 0);
textView.delegate = self;

Don’t forget to set the delegate, you have to implement UITextViewDelegate! Now we implement the delegate method:

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)url inRange:(NSRange)characterRange
{
     return YES;
}

This automatically opens the provided the NSURL-instance from the attributed string in my question on click/tap.

Remember: This works on iOS 7 only, for legacy support you need external libraries

UPDATE:

Making the UITextViews behave just like labels was a total mess in the end and i stumbled upon some hideous iOS-behaviours. I ended up using the TTTAttributedLabel library which is simply great and allowed me to use labels instead of UITextViews.

Leave a Comment