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>
source to share
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]]);
}
source to share