1

In one of my application, there is API called and get data from server. In that there is text - which contains emojis, special characters, URLs, etc.

Not In my UITableViewCell, I have Simple UILabel and set text to label. Till now everything is working fine.

But now I want that URLs in UILabel should be tappable, so when we click on URL and it should be open in Safari.

enter image description here

Note : As text is coming from server, so I don't know position of URL and there will be multiple URL as well.

What to do for that ? I have searched but didn't find solution.

VRAwesome
  • 4,721
  • 5
  • 27
  • 52
  • 1
    Check this SO question, it may help you https://stackoverflow.com/questions/1256887/create-tap-able-links-in-the-nsattributedstring-of-a-uilabel – Mani Oct 08 '18 at 06:14
  • 1
    check [TTTAttributedLabel](https://github.com/TTTAttributedLabel/TTTAttributedLabel) – ChintaN -Maddy- Ramani Oct 08 '18 at 06:18
  • 1
    you can also try YYText ( https://github.com/ibireme/YYText ) – Vatsal Shukla Oct 08 '18 at 06:34
  • @Mani I have checked, but I can't get the potion of URL in my case and there may be multiple URL as well. – VRAwesome Oct 08 '18 at 06:34
  • 1
    As from above UI, my best suggestion is `use UITextView` to do this job with less coding. You need to do some workaround to show it as a label. – Mani Oct 08 '18 at 06:40
  • In `UITextView`, wil it work automatic or need to do something ? – VRAwesome Oct 08 '18 at 06:45
  • 1
    Use a `UITextView`, cf https://developer.apple.com/videos/play/wwdc2018/221/ (look at 2:30). For link tap: https://stackoverflow.com/questions/2543967/how-to-intercept-click-on-link-in-uitextview You might have to change a little the UI of the `UITextView` to look like more like a `UILabel` but that should do it. – Larme Oct 08 '18 at 07:44

1 Answers1

1

I needed this kind of linked text for another project last month. I created a custom UITableViewCell with a UITextView object in the cell, subclassed to a custom UITextView subclass. I just made a quick demo project and put it up on git for you now. Feel free to use it.

github.com/fareast555/MPC_LinkedTextView

The basic secret sauce for using text links is the NSLinkAttributeName attribute in the mutable text, and telling the system to handle links.

- (void)updateTextViewWithFullText:(NSString *)fullText linkTriggerText:(NSString *)triggerText linkURLString:(NSString *)urlString
{
     //Create a mutable string based on the full text
     NSMutableAttributedString *mutableString = [[NSMutableAttributedString alloc]initWithString:fullText attributes:nil];

     //Add the link attribute across the range of the target text
     [mutableString addAttribute:NSLinkAttributeName value:urlString range:[fullText rangeOfString:triggerText]];

     //Add any other font or color bling as needed
     [mutableString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:18 weight:UIFontWeightMedium] range:NSMakeRange(0, [fullText length])];

     //Set the mutable text to the textfield
     [self setAttributedText: mutableString];
}

#pragma mark - UITextViewDelegate

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

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    return NO;
}

And for textviews, you'll find it easier to use auto layout if you turn off scrolling.

#pragma mark - Configure
- (void)_configureTextView
{
    self.delegate = self;
    [self setEditable:NO];
    [self setSelectable:YES];
    [self setScrollEnabled:NO];
    [self setUserInteractionEnabled:YES];
    [self setDataDetectorTypes:UIDataDetectorTypeLink];
    self.scrollIndicatorInsets = UIEdgeInsetsZero;
}

Screenshot of table view

Mike Critchley
  • 1,643
  • 15
  • 20