I have HTML code received from webservice. I need to trim it to simple text. But I need to display the links the html code is having. Is it possible? Here's my html code.
<div style="text-align: justify;"><img alt="Rangam" src="http://onboarding.rangam.com/Mybase/GetOrganizationLogo/1" style="height:80px; width:263px" /><br />
<p style="text-align:justify">Established in 1995, with offices in three continents, <a href="https://www.rangam.com" target="_blank">Rangam Consultants Inc.</a> is a high-performing diverse supplier of enterprise-wide staffing, payroll and on-boarding services. We are a certified WMBE that has consistently grown year-over-year and have an excellent history of client retention. We are proud that our clients consistently rate us among their top 5 service providers.</p>
<p style="text-align:justify">An expert workforce and cutting-edge technology solutions allow <a href="https://www.rangam.com" target="_blank">Rangam</a> to serve large clients nationwide. Our mature business processes have enabled us to successfully serve Fortune 500 corporations and the public sector.</p>
If I keep the html text as it is and add as attributed text in UITextview, it will look something like this. That i don't need. I need the text only to display with hyperlinks (image 2).
Create sample:
NSString *htmlStr = @"<div style=\"text-align: justify;\"><img alt=\"Rangam\" src=\"http://onboarding.rangam.com/Mybase/GetOrganizationLogo/1\" style=\"height:80px; width:263px\" /><br />\
<p style=\"text-align:justify\">Established in 1995, with offices in three continents, <a href=\"https://www.rangam.com\" target=\"_blank\">Rangam Consultants Inc.</a> is a high-performing diverse supplier of enterprise-wide staffing, payroll and on-boarding services. We are a certified WMBE that has consistently grown year-over-year and have an excellent history of client retention. We are proud that our clients consistently rate us among their top 5 service providers.</p>\
<p style=\"text-align:justify\">An expert workforce and cutting-edge technology solutions allow <a href=\"https://www.rangam.com\" target=\"_blank\">Rangam</a> to serve large clients nationwide. Our mature business processes have enabled us to successfully serve Fortune 500 corporations and the public sector.</p>";
NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithData:[htmlStr dataUsingEncoding:NSUTF8StringEncoding]
options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
documentAttributes:nil
error:nil];
We do the work here:
//We enumerate the attributes and we keep only the ones for NSLinkAttributeName
[attr enumerateAttributesInRange:NSMakeRange(0, [attr length]) options:0 usingBlock:^(NSDictionary<NSAttributedStringKey,id> * _Nonnull attrs, NSRange range, BOOL * _Nonnull stop) {
NSMutableDictionary *cleanDict = [[NSMutableDictionary alloc] init];
if ([attrs objectForKey:NSLinkAttributeName]) //There is a link
{
[cleanDict setObject:[attrs objectForKey:NSLinkAttributeName] forKey:NSLinkAttributeName];
//You may want to add effects like underline
if ([attrs objectForKey:NSUnderlineColorAttributeName])
[cleanDict setObject:[attrs objectForKey:NSUnderlineColorAttributeName] forKey:NSUnderlineColorAttributeName];
if ([attrs objectForKey:NSUnderlineStyleAttributeName])
[cleanDict setObject:[attrs objectForKey:NSUnderlineStyleAttributeName] forKey:NSUnderlineStyleAttributeName];
}
//We replace the whole attributes with the one we kept (either no attributes, or just the link ones
[attr setAttributes:cleanDict range:range];
}];
//We can also change the font if we want
[attr addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:14] range:NSMakeRange(0, [attr length])];
Result:
The first one is directly taken just after the creation of attr
, the other one after performing the modification. I set the font to bold to have a better view on changes.