Jayasabeen Appukuttan
Jayasabeen Appukuttan

Reputation: 1440

Remove html tags and display in UILabel Objective c

I am getting &nbsp /br> p> like Html tags in my api response. I want to display those contents in UILabel

What I did is:

NSString *STR_api = [NSString StingWithFormat@:"%@",[API_res valueforkey:@"description"]];
STR_api = [STR_api StringByreplacingaccurancesofString @"&nbsp" with string@""];

What I want is, In web portal it is displaying like bold, paragraph is it possible to display in UILabel by formatting the above response

Thanks in advance

Upvotes: 0

Views: 2784

Answers (3)

BSB
BSB

Reputation: 277

try this code:

NSString *strDescription = [NSString stringWithFormat:@"%@",[API_res valueforkey:@"description"]];

self.lblDescription.attributedText = [self getData:strDescription];

Convert HtmlString to string

-(NSAttributedString *)getData:(NSString *)str
{
    NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *options = @{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType};
    NSAttributedString *decodedString;
    decodedString = [[NSAttributedString alloc] initWithData:stringData
                                                     options:options
                                          documentAttributes:NULL
                                                       error:NULL];
    return decodedString;
}

Upvotes: 1

user3182143
user3182143

Reputation: 9599

I tried from perfect solution from Larme's answer

I got the solution.It works fine.

NSString *strHTML = @"S.Panchami 01.38<br>Arudra 02.01<br>V.08.54-10.39<br>D.05.02-06.52<br> <font color=red><u>Festival</u></font><br><font color=blue>Shankara Jayanthi<br></font>";
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[strHTML dataUsingEncoding:NSUTF8StringEncoding]
                                                            options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                                                      NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)}
                                                 documentAttributes:nil
                                                              error:nil];
NSLog(@"html: %@",strHTML);
NSLog(@"attr: %@", attrStr);
NSLog(@"string: %@", [attrStr string]);
NSString *finalString = [attrStr string];
NSLog(@"The finalString is - %@",finalString);

The printed results are

html

html: S.Panchami 01.38<br>Arudra 02.01<br>V.08.54-10.39<br>D.05.02-06.52<br> <font color=red><u>Festival</u></font><br><font color=blue>Shankara Jayanthi<br></font>

string

string: S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
Festival
Shankara Jayanthi

Final String

The finalString is - S.Panchami 01.38
Arudra 02.01
V.08.54-10.39
D.05.02-06.52
Festival
Shankara Jayanthi

Now for removing &nbsp from string

OPTION 1

NSRange range;
while ((range = [finalString rangeOfString:@"<[^>]+>" options:NSRegularExpressionSearch]).location != NSNotFound)
finalString = [finalString stringByReplacingCharactersInRange:range withString:@""];

OPTION 2

finalString = [finalString stringByReplacingOccurrencesOfString:@"&nbsp" withString:@""];

Remove HTML Tags from String

Upvotes: 5

Milap Kundalia
Milap Kundalia

Reputation: 1606

Use below UILabel formatting control which provides Rich text formatting based on HTML-like markups for iOS. RTLabel

Upvotes: 0

Related Questions