0

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).

enter image description here

enter image description here

Larme
  • 24,190
  • 6
  • 51
  • 81
Krutika Sonawala
  • 1,065
  • 1
  • 12
  • 30
  • did you check these links 1. https://stackoverflow.com/questions/19226634/converting-html-text-into-plain-text-using-objective-c 2. https://stackoverflow.com/questions/8959700/how-to-convert-html-tags-to-plain-text-in-iphone 3. https://gist.github.com/drewmccormack/3840090 for link 1. https://stackoverflow.com/questions/10243585/html-parsing-how-to-get-link-tag-from-remote-site 2. https://stackoverflow.com/questions/6906999/how-do-i-parser-html-code-i-want-get-tag-in-html-code-for-iphone 3. https://stackoverflow.com/questions/16376982/objective-c-html-parsing-get-all-text-between-tags – Gagan_iOS Oct 23 '17 at 11:13
  • these links are trimming the whole html text to plain text. I need to keep the hyperlink tag within the text. – Krutika Sonawala Oct 23 '17 at 11:15
  • what is your actual requirement? I think you want to show link as highlighted in your textview/textfield/Label. – Gagan_iOS Oct 23 '17 at 11:19
  • yes, show links as highlighted and it should redirect to particular pages – Krutika Sonawala Oct 23 '17 at 11:20
  • what are you using? Textfield or textview or something else. I can provide another way – Gagan_iOS Oct 23 '17 at 11:21
  • I am using UITextView to display that text – Krutika Sonawala Oct 23 '17 at 11:23
  • ohk..thats great. there is the simplest way to do this from storyboard (If you are using). See in this link https://stackoverflow.com/questions/34425096/how-to-display-clickable-links-in-uitextview you have to do nothing just select option in Storyboard and done. – Gagan_iOS Oct 23 '17 at 11:25
  • Yes, that is the way I know. But my HTML string is having an image and everything that i do not need to display. without removing html formatting, the texts looks so small. That is covered by removing html and displaying them as a simple text. let me give the image in the question. – Krutika Sonawala Oct 23 '17 at 11:39
  • remove all your unnecessary tags and get a plain string. Now perform text formatting (font, size etc) and assing that string into UItextView – Gagan_iOS Oct 23 '17 at 11:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/157279/discussion-between-krutika-sonawala-and-gagan-ios). – Krutika Sonawala Oct 23 '17 at 11:44
  • 1
    i would suggest you to use [TTTAttributedLabel](https://github.com/TTTAttributedLabel/TTTAttributedLabel) – Pratik Jamariya Oct 24 '17 at 07:32

1 Answers1

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.

enter image description here

Larme
  • 24,190
  • 6
  • 51
  • 81
  • @Lame salute !! – Krutika Sonawala Oct 24 '17 at 08:08
  • @Lame can you please make me understand this code? I am unable to grasp this method. And I am getting the warnings for sending NSString* to parameter of type NSAttributedString*. – Krutika Sonawala Oct 24 '17 at 08:40
  • What part? How to use `enumerateAttributesInRange: options :usingBlock:`? It's an UIKit method. Look at its doc. I commented to explain what I was doing. In other words, I just keep the "Links effects" and remove the other effects. – Larme Oct 24 '17 at 08:41