0

I've got some text coming from my server, some of which is HTML. I was able to use the attributed string and initWithData to format the text for display so that Apple shows as Apple and blue and underlined. But I couldn't detect the tap. I used TTTAttributedLabel and was able to write a dummy string and have it create a link whose tap I intercepted with didSelectLinkWithURL. Problem is, that method of defining the range of the text assumes you know the text of the link. I'm getting different strings each time, and I won't know what the string is to add the link to. Should I look for a tag opening and closing? Also, can TTT handle if the string has more than one link embedded in it?

skinsfan00atg
  • 571
  • 1
  • 3
  • 19
  • You need to parse your HTML response into an NSAttributedString that can be displayed by the label. You'll need to extract the links from your HTML string and add a URL to each range of text in your label that corresponds to a link. There are numerous open-source libraries that will do exactly this. And yes, the label can have multiple links. See the various addLink... methods. – Jonathan Hersh Jun 20 '15 at 21:19

2 Answers2

1

Here is the solution.. First setup your attributed text. Then find the links for each character. Then setup them with addLinkToUrl method.

NSAttributedString *attrString = [[NSAttributedString alloc] initWithData: 
                                   [text dataUsingEncoding:NSUTF8StringEncoding]   
                                   options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                                    documentAttributes:nil error:nil];
[label setAttributedText: attrString];
    
if (label.attributedText != nil && label.attributedText.length > 0) {
    for (int i= 0; i < label.attributedText.length; i++) {
        NSRange range = NSMakeRange(0,1);
        NSDictionary<NSAttributedStringKey,id> *dict = [label.attributedText attributesAtIndex:i effectiveRange:&range];
        if (dict[NSLinkAttributeName] != nil) {
            NSURL *url = dict[NSLinkAttributeName];
            [label addLinkToURL:url withRange:range];
        }
    }
}

After you do this you will be able to detect link taps with TTTAttributedLabelDelegate

- (void)attributedLabel:(TTTAttributedLabel *)label didSelectLinkWithURL:(NSURL *)url {
    if([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    } 
}
Gokhan Alp
  • 61
  • 5
0

Display text in html code in UIWebview.

<html>
<body>
<p><a href="http://www.w3schools.com/html/">Your text</a></p>
</body>
</html>

Load aboue text in the UIwebview

  • thanks so much for the response. web view the only way? I've got everything setup with the label already. can the web view show some normal text in addition to the links? sample text i get from the server is something like this: "

    Doctors look at new study for answers in the fight against cancer.

    – skinsfan00atg Jun 16 '15 at 17:43