cocoa - TextView 中的 HTML

标签 cocoa

我在 cocoa 中使用textview 我们如何在textview中显示HTML数据

最佳答案

您没有说您的目标是 Mac 还是 iPhone。因为您用 cocoa 标记了您的问题,所以我假设是 Mac。

值得研究一下 Text System documentation了解 NSTextView 工作原理的概述。但在基本层面上,所有 NSTextView 都有一个 NSTextStorage 对象(可通过 textStorage 方法访问)NSTextStorage 发生成为 NSMutableAttributedString 的子类。

NSAttributedString 有一个名为 initWithHTML:baseURL:documentAttributes: 的初始值设定项它会为你解析一个 HTML 字符串。完成后,您可以将生成的字符串附加到 NSTextViewtextStorage 中。例如:

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/

相关文章:

objective-c - 从 Cocoa IBAction 启动 WebKit Inspector?

mysql - NSArray 从 MySQL 到 NSTableView

objective-c - 自定义NSMenu(像Apple的Dock右键菜单)?

macos - Mac 沙盒和临时文件

objective-c - 如何从 osx 应用程序播放系统声音?

objective-c - 多窗口应用程序

cocoa - 如何从 AppDelegate 获取特定 Controller

macos - Qt + CMake for OSX bundle : Cocoa Platform Plugin

cocoa - 当 WebView 添加到 NSWindow 中的特定 View 时,:hover stops working. 如何修复此问题?

objective-c - 阻止用户使用桌面、文档、下载、电影、音乐、图片、公共(public)文件夹