3

I'm developing an iOS app for a news website, the app gets the content from a URL (JSON).

Every article in the website has a description and in which there might be embedded images. How can I display the embedded HTML images in my UITextView (or maybe UILabel?) ? I have found a class named: RichContentLabel but it isn't compatible with iOS7+. Is there another similar class I could use?

This is a test string with embedded images:

<div> some text here <span> something else</span>
<img src="image source" /> something here too
</div>
Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
ANA
  • 282
  • 2
  • 16
  • Check out this answer - http://stackoverflow.com/questions/4217820/convert-html-to-nsattributedstring-in-ios – Asaf May 27 '15 at 13:51

2 Answers2

1

If you don't want to use a UIWebView you will need to find a way to get the source URL for the image from your HTML string.

You could do this with NSRegularExpression and then load the imageURL into a UIImageView

EDIT:

I used this website to try my regex.

NSError *error = nil;
NSString *regExPattern = @"img src=\"(.*?)\"";
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regExPattern
                                                                       options:NSRegularExpressionCaseInsensitive | NSRegularExpressionDotMatchesLineSeparators
                                                                         error:&error];
NSArray *matches = [regex matchesInString:HTMLString options:0 range:NSMakeRange(0, HTMLString.length)];


for (NSTextCheckingResult *result in [matches reverseObjectEnumerator])
{
    NSLog(@"0 %@", [HTMLString substringWithRange:[result rangeAtIndex:0]]);
}
Jasper
  • 7,031
  • 3
  • 35
  • 43
  • yes doing that with one image is somehow easy, but what if there are multiple images in the strings and in multiple places, how can you get all the images and place them in their appropriate places? – ANA May 27 '15 at 14:48
  • Thats what `NSRegularExpression` is for. You will get an array of results. You could then add the images to a `UIScrollView` of `UIImageView`'s – Jasper May 27 '15 at 14:52
0

You could use a UIWebView:

UIWebView *myWebView = [UIWebView alloc] init];

[myWebView loadHTMLString:testString baseURL:nil];
Jasper
  • 7,031
  • 3
  • 35
  • 43
  • Thanks i know that, but I don't want to use UIWebView because it's laggy on old devices, plus having a full native app is always better. – ANA May 27 '15 at 14:09