Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to display rich content in an iOS app?

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>
like image 322
ANA Avatar asked Jan 22 '26 13:01

ANA


1 Answers

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]]);
}
like image 114
Jasper Avatar answered Jan 25 '26 12:01

Jasper