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

I am developing an iOS app for a news site, the app gets content from url (JSON).

Each article on the site has a description and may contain embedded images. How can I display embedded HTML images in a UITextView (or perhaps a UILabel?)? I found a class called: RichContentLabel , but it is not compatible with iOS7 +. Is there any other similar class I could use?

This is a test line with inline images:

<div> some text here <span> something else</span>
<img src="image source" /> something here too
</div>

      

+3


source to share


2 answers


You can use UIWebView

:



UIWebView *myWebView = [UIWebView alloc] init];

[myWebView loadHTMLString:testString baseURL:nil];

      

0


source


If you don't want to use UIWebView

, you will need to find a way to get the original url for the image from your HTML string.

You can do it with NSRegularExpression

and then load the imageURL intoUIImageView

EDIT:



I used this site 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]]);
}

      

0


source







All Articles