The Application Kit’s extensions to NSAttributedString add support for reading and writing text formatting commands and document attributes for a number of popular markup languages, including the following:
- Application Kit对NSAttributedString的扩展增加了对许多流行标记语言的文本格式化命令和文档属性的读写支持,包括:
-
HTML
-
RTF and RTFD
-
Microsoft Word
-
Word XML
-
WebKit WebArchive
-
ECMA Office Open XML
-
OASIS Open Document
These document formats are represented by the values returned for the NSDocumentTypeDocumentAttribute key in the document attributes dictionary when reading and writing text documents. For all of these languages, files representing documents include both the text to be displayed and interspersed formatting commands. Programs that display the documents interpret the commands to format the text. Formatting commands or “tags” represent such formatting elements as paragraphs, headings, line breaks, images, hyperlinks, and so on. In addition, some commands represent document-wide attributes, such as paper size, margins, background color, and so on.
- 这些文档格式由读取和写入文本文档时在文档属性字典中为NSDocumentTypeDocumentAttribute键返回的值表示。 对于所有这些语言,表示文档的文件包括要显示的文本和散布的格式化命令。 显示文档的程序解释用于格式化文本的命令。 格式化命令或“标签”表示诸如段落,标题,换行符,图像,超链接等格式化元素。 此外,某些命令表示文档范围的属性,例如纸张大小,边距,背景颜色等。
For additional information specific to RTF and RTFD, see RTF Files and Attributed Strings.
- 有关RTF和RTFD的其他信息,请参阅RTF文件和归属字符串。
Reading Formatted Documents
The Application Kit’s extensions to NSAttributedString
include two general-purpose methods to create an attributed string by loading text documents in various formats. The document format is specified in an options dictionary containing keys described in “Option keys for importing documents” in NSAttributedString AppKit Additions Reference. If the NSDocumentTypeDocumentOption key is specified, with one of the values defined for NSDocumentTypeDocumentAttribute, the document is interpreted according to the specified format. If the NSDocumentTypeDocumentOption key is not specified, the general methods examine the document and perform a best effort to load it using the appropriate format.
- Application Kit对NSAttributedString的扩展包括两种通用方法,通过以各种格式加载文本文档来创建属性字符串。 文档格式在包含NSAttributedString AppKit Additions Reference中“导入文档的选项键”中描述的键的选项字典中指定。 如果指定了NSDocumentTypeDocumentOption键,并且为NSDocumentTypeDocumentAttribute定义了其中一个值,则将根据指定的格式解释文档。 如果未指定NSDocumentTypeDocumentOption键,则常规方法将检查文档并尽最大努力使用适当的格式加载它。
The two general methods for reading formatted documents are:
- 阅读格式化文档的两种通用方法是:
| initWithURL:options:documentAttributes:error: | |
| initWithData:options:documentAttributes:error: | |
The Application Kit’s extensions to NSAttributedString
also define a number of special-purpose convenience methods to create an attributed string for various common document types. Also, NSMutableAttributedString
defines the following methods:
- Application Kit对NSAttributedString的扩展还定义了许多特殊用途的便捷方法,用于为各种常见文档类型创建属性字符串。 此外,NSMutableAttributedString定义了以下方法:
| readFromURL:options:documentAttributes: | |
| readFromData:options:documentAttributes: | |
- Application Kit对NSAttributedString的扩展还定义了许多特殊用途的便捷方法,用于为各种常见文档类型创建属性字符串。 此外,NSMutableAttributedString定义了以下方法:
Handling Document Attributes
Attributed strings store attribute information for characters and paragraphs only, but most document formats also support more general attributes of a document, such as paper size and page layout. The NSAttributedString
reading and writing methods store these directives in a document attributes dictionary. If the document attributes dictionary passed with a document-reading method is not NULL
, it is populated with various document-wide attributes. The document attributes supported vary according to document type. Possible document attribute keys and the values they can take are described in “Document Attributes” in NSAttributedString AppKit Additions Reference.
- 归属字符串仅存储字符和段落的属性信息,但大多数文档格式还支持文档的更多常规属性,例如纸张大小和页面布局。 NSAttributedString读取和写入方法将这些指令存储在文档属性字典中。 如果使用文档读取方法传递的文档属性字典不是NULL,则会使用各种文档范围的属性填充它。 支持的文档属性因文档类型而异。 可能的文档属性键及其可以采用的值在NSAttributedString AppKit Additions Reference中的“文档属性”中描述。
Writing Formatted Documents
The Application Kit’s extensions to NSAttributedString
also define methods to produce data for saving text documents in various formats. These methods take a document attributes dictionary to enable writing out various document-wide attributes, and the attributes supported vary by document type, as described for the document-reading methods.
- Application Kit对NSAttributedString的扩展还定义了生成用于以各种格式保存文本文档的数据的方法。 这些方法采用文档属性字典来编写各种文档范围的属性,并且支持的属性因文档类型而异,如文档读取方法所述。
The first two methods are general, that is, applicable to any supported document type. They require a document attributes dictionary specifying at least the NSDocumentTypeDocumentAttribute to determine the format to be written.
- 前两种方法是通用的,即适用于任何支持的文档类型。 它们需要一个文档属性字典,至少指定NSDocumentTypeDocumentAttribute以确定要写入的格式。
The two general methods for writing formatted documents are:
- 编写格式化文档的两种通用方法是:
| dataFromRange:documentAttributes:error: | |
| fileWrapperFromRange:documentAttributes:error: | |
Use dataFromRange:documentAttributes:error: to create a data object that can be written to a regular file on disk. Use fileWrapperFromRange:documentAttributes:error: when you want to create a directory structure on disk, such as RTFD. The file wrapper method returns a directory file wrapper for those document types for which it is appropriate; otherwise it returns a regular-file file wrapper.
- 使用dataFromRange:documentAttributes:error:创建一个可以写入磁盘上常规文件的数据对象。 使用fileWrapperFromRange:documentAttributes:error:当您想在磁盘上创建目录结构时,例如RTFD。 文件包装器方法返回适合的文档类型的目录文件包装器; 否则它返回一个常规文件文件包装器。
The Application Kit’s extensions to NSAttributedString
also define a number of special-purpose convenience methods to produce data for writing various common document types.
- Application Kit对NSAttributedString的扩展还定义了许多特殊用途的便捷方法,用于生成用于编写各种常见文档类型的数据。
Handling Attachments
- 处理附件
Attachments, such as embedded images or files, are represented in an attributed string by both a special character and an attribute. The character is identified by the global name NSAttachmentCharacter
(U+FFFC
, the Unicode replacement character), and indicates the presence of an attachment at its location in the string. The attribute, identified in the string by the attribute name NSAttachmentAttributeName
, is an NSTextAttachment
object . An NSTextAttachment
object contains the data for the attachment itself and an image to display when the string is drawn.
- 附件(例如嵌入的图像或文件)由特殊字符和属性在属性字符串中表示。 该字符由全局名称NSAttachmentCharacter(U + FFFC,Unicode替换字符)标识,并指示字符串中其位置是否存在附件。 由属性名称NSAttachmentAttributeName在字符串中标识的属性是NSTextAttachment对象。 NSTextAttachment对象包含附件本身的数据以及绘制字符串时要显示的图像。
You can use the NSAttributedString
method attributedStringWithAttachment: class method to construct an attachment string, which you can then add to a mutable attributed string using appendAttributedString: or insertAttributedString:atIndex:.
- 您可以使用NSAttributedString方法attributesStringWithAttachment:class方法构造附件字符串,然后可以使用appendAttributedString:或insertAttributedString:atIndex:将其添加到可变属性字符串中。
网友评论