Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSAttributedString is not rendering html line breaks

I am using attributedText property of label to render html.

It is working fine with rendering bold and italic. However when i try to render html line breaks it just cuts off all the text appearing after the line break.

NSString* view = @"<i>Testing</i> and this is <b>bold</b> <br> This should be in next line";
NSDictionary *options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};

NSAttributedString *preview = [[NSAttributedString alloc] initWithData:[view dataUsingEncoding:NSUTF8StringEncoding] options:options documentAttributes:nil error:nil];
self.labelView.attributedText = preview;

Help is required

like image 897
Win Coder Avatar asked Dec 14 '25 06:12

Win Coder


2 Answers

As mentioned in the comments, for me this issue was not with HTML rendering but with forgetting to set "Number of Lines" to 0 on the UILabel.

like image 166
gregkerzhner Avatar answered Dec 16 '25 21:12

gregkerzhner


I tried and this worked

NSString* view = @"<i>Testing</i> and this is <b>bold</b> <br> This should be in next line";
NSAttributedString *preview =[[NSAttributedString alloc] initWithData:[view dataUsingEncoding:NSUTF8StringEncoding]
                                 options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType,
                                           NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
                      documentAttributes:nil error:nil];

UILabel *lbl = [[UILabel alloc] initWithFrame:cell.textLabel.frame];
[lbl setAttributedText:preview];
like image 36
Debanjan Chakraborty Avatar answered Dec 16 '25 20:12

Debanjan Chakraborty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!