3

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

Win Coder
  • 6,628
  • 11
  • 54
  • 81
  • Maybe you're missing the NSUTF8StringEncoding for character encoding part as shown here: http://stackoverflow.com/questions/4217820/convert-html-to-nsattributedstring-in-ios `[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];` – Zhang Nov 06 '14 at 06:57
  • what encoding should i use then as the html would not a webpage but would be primarily for formatting text – Win Coder Nov 06 '14 at 06:59
  • Does replacing `@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType};` with `@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)};` help? – Zhang Nov 06 '14 at 07:00
  • Actually, what happens if you replace `
    ` with `
    `? I think `
    ` is the proper XHTML tag for line breaks.
    – Zhang Nov 06 '14 at 07:02
  • 3
    Actually the code in the question is working fine i just forgot the change the number of lines for uilabel in Interface Builder. Thanks for the help. – Win Coder Nov 06 '14 at 07:05

3 Answers3

3

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.

gregkerzhner
  • 736
  • 7
  • 18
2

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];
2

I am having some issues regarding This and getting ASCII codes instead of some special characters like '&' and some other codes . All issues solved by just using.:

NSString *html = [command.arguments objectAtIndex:
NSData *data =  [html dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = @{@"WebMainResource": @{@"WebResourceData": data, @"WebResourceFrameName": @"", @"WebResourceMIMEType": @"text/html", @"WebResourceTextEncodingName": @"UTF-8", @"WebResourceURL": @"about:blank"}};
data = [NSPropertyListSerialization dataWithPropertyList:dict format:NSPropertyListXMLFormat_v1_0 options:0 error:nil];
NSAttributedString *decodedString = [[NSAttributedString alloc] initWithData:data
                                                                        options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType}
                                                                  documentAttributes:NULL
                                                                               error:NULL];`
Adis
  • 4,512
  • 2
  • 33
  • 40
Anuj Raghuvanshi
  • 664
  • 1
  • 7
  • 24