iOS等同于MacOS NSAttributedString initWithRTF

iOS等同于MacOS NSAttributedString initWithRTF

问题描述:

什么是与MacOS NSAttributedString initWithRTF等效的iOS?

What is an iOS equivalent to MacOS NSAttributedString initWithRTF ?

该应用程序套件通过添加对RTF,RTFD和HTML(带有或不带有附件),图形属性(包括字体和标尺属性),绘制属性字符串的方法以及计算重要语言单元的方法的支持,扩展了Foundation的NSAttributedString类.

The Application Kit extends Foundation’s NSAttributedString class by adding support for RTF, RTFD, and HTML (with or without attachments), graphics attributes (including font and ruler attributes), methods for drawing attributed strings, and methods for calculating significant linguistic units.

- (id)initWithRTF:(NSData *)rtfData documentAttributes:(NSDictionary **)docAttributes

我需要在iOS应用程序中处理一小段RTF数据流. 谢谢!

I need to process a short stream of RTF data in an iOS application. Thank you!

iOS中没有等效的解决方案. iOS版本的NSAttributedString仅具有CoreText所需的功能,而CoreText本身仅具有UI类所需的功能,并且不包括RTF处理.

There is no equivalent in iOS. The iOS version of NSAttributedString has only the functionality needed by CoreText, and CoreText itself has only the functionality needed by the UI classes, and this does not include RTF processing.

据我所知,至少从5.0开始,UIWebView是处理RTF的唯一方法.参见 https://developer.apple.com/library/ios/# qa/qa1630/_index.html .在5.1版本中情况可能有所改变,因为苹果的其他一些应用程序现在似乎可以处理富文本了(但同样,这些应用程序可能刚刚从UITextView更改为UIWebView ...).

As far as I know, at least as of 5.0, UIWebView is the only way to process RTF. See https://developer.apple.com/library/ios/#qa/qa1630/_index.html. That may have changed with 5.1, because some of Apple's other apps seem to handle rich text now (but then again, those apps may have just changed from UITextView to UIWebView…).

UIWebView没有提供任何从RTF访问属性文本的方法,因为Web视图从不构建属性文本,而是构建HTML和CSS.

UIWebView doesn't give you any way to access the attributed text from the RTF, because the web view never builds attributed text—instead, it builds HTML and CSS.

好消息是,此HTML和CSS在DOM中可用.至少对于某些类型,这是不正确的—您看到的是一个不透明的包装,该包装由WebKit中无法访问的魔术代码渲染—但是RTF不是这些类型之一.

The good news is that this HTML and CSS is available in the DOM. At least for some types, this isn't true—all you see is an opaque wrapper that gets rendered by some magic code inside WebKit that you can't access—but RTF is not one of those types.

使用 RTF示例文件在我的iPhone上,然后查看了DOM.到处都是这样的节点:

Using Safari Web Inspector, I opened up a sample RTF file on my iPhone, and looked at the DOM. It's full of nodes like this:

<span class="s2"><span class="bumpedFont16">m a test file. This is some </span></span>
<span class="s3"><span class="bumpedFont16">bold</span></span>

…,其中那些类是在首次使用它们的标记之前紧接内联<style>标记定义的.

… where those classes are defined with inline <style> tags immediately before the tag they're first used in.

因此,如果遍历具有文本的节点,则该文本就像是一个归属的字符范围(尽管如果您实际上想要开始索引和结束索引,则需要自己计算……),其计算样式大致就像属性字典一样.当然,大致上喜欢"不是相同".计算出的样式如下所示:

So, if walk the nodes that have text, that text is like an attributed character range (although if you actually want the start and end indices you'll need to count them up yourself…), and its computed style is roughly like an attribute dictionary. Of course "roughly like" isn't "identical"; a computed style looks like this:

direction: ltr; display: inline; font-family: 'Times New Roman'; font-size: 22px;
font-weight: bold; height: auto; margin-bottom: 0px; margin-top: 0px;
padding-right: 0px; text-align: left; width: auto;

因此,实际上与NSAttributedString不兼容,并且也不尽如人意(当然,除非最终目标是创建HTML)…但是至少可以使用某些东西.

So, not actually compatible with NSAttributedString, and not nearly as nice (unless your end goal is to create HTML, of course)… but at least something you can use.