-1

I'm using JSON in my app to call elements from a database.
One of these elements is a text block with href links.

The JSON looks like :

"textBlock":"<a href=\"http:\/\/www.website.com\/" target=\"_blank\">Link<\/a>

In my app I call label with :

self.TextLabel.text = self.item[@"textBlock"];
[selfTextLabel sizeToFit];

Result in my app shows :

<a href="http://www.website.com/" target="_blank">Link</a>

Would it be possible to write / strip this link properly ?

I came across this solution to strip the html, which works fine, but my links don't work, I would like to know if I can keep my links working.

Community
  • 1
  • 1
David
  • 213
  • 3
  • 10
  • Take a look in the regex ? – David Ansermot Apr 29 '15 at 12:58
  • Sorry if I don't seem to show any research effort here, but I can't find what I need, that's why I posted. – David Apr 29 '15 at 13:06
  • possible duplicate of [Remove HTML Tags from an NSString on the iPhone](http://stackoverflow.com/questions/277055/remove-html-tags-from-an-nsstring-on-the-iphone) – David Ansermot Apr 29 '15 at 13:08
  • 1
    OK, thanks for the link, I'll check that right away. – David Apr 29 '15 at 13:09
  • 1
    Thanks again, I think I actually allready came across that link, but was a bit affraid of release and dealloc which looked oldschool. I used Leigh McCulloch answer, works nice with some changes, only thing is that links don't work, but I will check a bit further. – David Apr 29 '15 at 13:17

1 Answers1

1

OK, so after some more searching and trying, I finally got what I needed.

I first tried to put my string in UITextView, selectable with links detection. Would have been great if I had written directly my URLs in the text.

But again, the strings I receive from JSON look like :

<a href="http://www.website.com/" target="_blank">Link</a>

I looked at Fancy UILabels and NSDataDetector, but it seemed like the labels were working but still showing http:// which looked not good for me.

So I figured best way was to put this string in a UIWebView, and call it like (I replaced TextLabel in the question with TextView).

[self.TextView loadHTMLString:self.item[@"textBlock"] baseURL:nil];

I finally had some last issue, as the links were opening in the UIWebView instead of Safari.

So I added self.TextView.delegate = self; in viewDidLoad.

And

-(BOOL) webView:(UIWebView *)TextView shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
    }

    return YES;
}

.h file must also call UIWebViewDelegate.

And if you think UIWebView default font is ugly in this case, like I did, you can do :

NSString *nicerTextBlock = self.item[@"textBlock"];
[self.textView loadHTMLString:[NSString stringWithFormat:@"<style type='text/css'>body { font-family: Helvetica; font-size: 12 } ></style>%@", nicerTextBlock] baseURL:nil];

Hope this can spare some time for other people.

David
  • 213
  • 3
  • 10