我在 cocoa 中使用textview 我们如何在textview中显示HTML数据
最佳答案
您没有说您的目标是 Mac 还是 iPhone。因为您用 cocoa 标记了您的问题,所以我假设是 Mac。
值得研究一下 Text System documentation了解 NSTextView
工作原理的概述。但在基本层面上,所有 NSTextView
都有一个 NSTextStorage
对象(可通过 textStorage
方法访问)NSTextStorage
发生成为 NSMutableAttributedString
的子类。
NSAttributedString
有一个名为 initWithHTML:baseURL:documentAttributes:
的初始值设定项它会为你解析一个 HTML 字符串。完成后,您可以将生成的字符串附加到 NSTextView
的 textStorage
中。例如:
NSData *htmlData = // get the html, e.g. from a file
NSURL *aBaseURL = // a URL used to resolve relative links e.g. '/directory/page.html'
NSAttributedString *formattedHTML = [[NSAttributedString alloc] initWithHTML:htmlData baseURL:aBaseURL documentAttributes:NULL];
[[myTextView textStorage] appendAttributedString:formattedHTML];
[formattedHTML release];
关于cocoa - TextView 中的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1860129/