【问题标题】:NSAttributedString is not rendering html line breaksNSAttributedString 不呈现 html 换行符
【发布时间】:2014-11-06 06:50:23
【问题描述】:

我正在使用labelattributedText 属性来呈现html。

渲染粗体和斜体效果很好。但是,当我尝试呈现 html 换行符时,它只会切断换行符后出现的所有文本。

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;

需要帮助

【问题讨论】:

  • 也许您缺少字符编码部分的 NSUTF8StringEncoding,如下所示:stackoverflow.com/questions/4217820/…[[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];
  • 我应该使用什么编码,因为 html 不是网页,而是主要用于格式化文本
  • @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}; 替换@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType}; 有帮助吗?
  • 实际上,如果将&lt;br&gt; 替换为&lt;br /&gt; 会发生什么?我认为&lt;br /&gt; 是适合换行符的 XHTML 标记。
  • 实际上问题中的代码运行良好,我只是忘记了在 Interface Builder 中更改 uilabel 的行数。感谢您的帮助。

标签: ios objective-c uilabel nsattributedstring


【解决方案1】:

正如 cmets 中提到的,对我来说,这个问题不在于 HTML 渲染,而是忘记在 UILabel 上将“行数”设置为 0。

【讨论】:

    【解决方案2】:

    我试过了,效果很好

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

    【讨论】:

      【解决方案3】:

      我遇到了一些关于 This 和获取 ASCII 代码而不是一些特殊字符(如 '&amp;' 和其他一些代码)的问题。只需使用即可解决所有问题。:

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

      【讨论】:

        猜你喜欢
        • 2012-09-23
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 2014-05-04
        • 2015-11-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多