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>
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]]);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With