NSAttributedString用图像加载html

时间:2014-07-22 12:33:31

标签: html ios7 uitextview nsattributedstring

我正在尝试在iOS 7中使用NSAttributedString呈现html文档(作为对UIWebView替代品的调查的一部分)。代码如下:

NSData *htmlData = [NSData dataWithContentsOfFile:self.chapterPath];

NSAttributedString *attrString = [[NSAttributedString alloc] initWithData:htmlData
                                                                  options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} 
                                                       documentAttributes:nil error:nil];

self.readerTextView.attributedText = attrString;
self.readerTextView.editable = NO;

没有显示图像。是否可以使用此方法使用图像呈现HTML,或者我是否错过了一些选项或属性?感谢。

更新

一些未显示的标签:

<img src="images/cover.jpg" alt="cover" style="height: 100%"/>

2 个答案:

答案 0 :(得分:1)

可以使用NSAttributedString使用图像呈现HTML。问题是在“src”属性中我认为相对链接不起作用。通过file://或通过http / https访问互联网资源的完整链接是可以接受的。

文章与工作实例:

http://initwithfunk.com/blog/2013/09/29/easy-markdown-rendering-with-nsattributedstring-on-ios-7/ https://github.com/ericallam/MarkyMark

答案 1 :(得分:0)

在iOS 14上包含标记似乎可以,但在Mac Catalyst 14上则不能,NSAttributedString调用失败。从NSAttributedString的文档中:“从主线程调用它是可行的(但如果HTML包含对外部资源的引用,仍然会超时,应不惜一切代价避免。)HTML导入机制旨在实现诸如markdown(即文本样式,颜色等),而不是用于常规HTML导入。”

相关问题