Reputation:
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.
Upvotes: 5
Views: 2012
Reputation: 13744
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:)
Upvotes: 2
Reputation: 798
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];
Upvotes: 1