Rich Text Format (RTF) is a text formatting language devised by Microsoft Corporation. You can represent character, paragraph, and document format attributes using plain text with interspersed RTF commands, groups, and escape sequences. RTF is widely used as a document interchange format to transfer documents with their formatting information across applications and computing platforms. The Application Kit has support for reading and writing RTF. For text attributes not available in standard RTF, Apple has extended RTF with custom commands.
Reading and Writing RTF Data
Apple’s RTF Extensions
The Application Kit’s extensions for NSAttributedString
add support for reading text attributes from, and writing them to, RTF files or RTFD (rich text with attachments) files.
Important: The Application Kit extensions write the standard character-level attributes from the attributed string and the standard document-level attributes from the document attributes dictionary; however, custom attributes that you define and add to an attributed string are not written to the RTF file. Standard character-level attribute keys are described in “Standard Attributes,” and the document attributes are described in Table 1.
The NSAttributedString
methods for writing rich text are defined in NSAttributedString Application Kit Additions Reference:
RTFFromRange:documentAttributes: | Returns an NSData object that contains an RTF stream corresponding to the characters and attributes within the given range, omitting all attachment attributes. |
RTFDFromRange:documentAttributes: | Returns an NSData object that contains an RTFD stream corresponding to the characters and attributes within aRange. |
RTFDFileWrapperFromRange:documentAttributes: | Returns an NSFileWrapper object that contains an RTFD document corresponding to the characters and attributes within the given range. |
initWithRTF:documentAttributes: | Initializes a new NSAttributedString object by decoding the stream of RTF commands and data contained in the given data object. |
initWithRTFD:documentAttributes: | Initializes a new NSAttributedString object by decoding the stream of RTFD commands and data contained in the given data object. |
initWithRTFDFileWrapper:documentAttributes: | Initializes a new NSAttributedString object from the given NSFileWrapper object containing an RTFD document. |
In addition to these explicit RTF-reading methods, four methods implicitly allow loading RTF data from a file or URL-specified resource. NSAttributedString
defines:
initWithPath:documentAttributes: | Initializes a new NSAttributedString object from RTF or RTFD data contained in the file at the given path. |
initWithURL:documentAttributes: | Initializes a new NSAttributedString object from the data at the given URL. |
NSMutableAttributedString
defines:
readFromURL:options:documentAttributes: | Sets the contents of receiver from the file at url. |
readFromData:options:documentAttributes: | Sets the contents of the receiver from the stream at data. |
Attributed strings store attribute information for characters and paragraphs only, while RTF also supports more general attributes of a document, such as paper size and page layout. The Application Kit methods that work with RTF read and write some RTF directives for document attributes, stored in an NSDictionary
object.
Many init
methods return a dictionary containing the attributes read from RTF data, which you can use to set up a page layout. Similarly, RTF extraction methods such as RTFFromRange:documentAttributes:
, accept a dictionary containing those attributes and write them into the RTF data, thus preserving the page layout information.
Table 1 lists the RTF document attributes supported by the Application Kit.
Attribute Key |
Type |
---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
, 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, as well as an image to display when the string is drawn.
You can use NSAttributedString
’s attributedStringWithAttachment:
class method to construct an attachment string, which you can then add to a mutable attributed string using appendAttributedString:
or insertAttributedString:atIndex:
. To write rich text data containing one or more attachments, use the RTFDFromRange:documentAttributes:
method and the RTFDFileWrapperFromRange:documentAttributes:
method. To initialize an attributed string with rich text data containing attachments, use the initWithRTFD:documentAttributes:
, and initWithRTFDFileWrapper:documentAttributes:
methods.
Apple has extended the RTF language to support text attributes and formatting constructs available in the Cocoa text system but not representable with standard RTF. The Apple extensions take the same form as standard RTF commands, groups, and escapes. RTF commands consist of a backslash followed by a string of alphabetic characters (case sensitive) followed by an optional integer parameter value which can be positive or negative. RTF groups begin with a left brace ({), followed by RTF sequences optionally including other groups, closed by a right brace (}). RTF escapes consist of a backslash followed by a special character, such as \{
, which indicates a literal left brace instead of the beginning of a group.
RTF includes the concept of a destination, which is a group containing an RTF command and text possibly to be inserted at a different location in a document, such as a footnote. The escape sequence \*
indicates that RTF readers that don’t understand the command that follows should ignore the contents of the destination.
Dimensions in RTF are expressed in twips—one twip is one twentieth of a point.
Table 2 lists Apple’s RTF extensions for character attributes.
RTF Sequence |
Description |
Parameter(s) |
---|---|---|
\CocoaLigatureN |
Ligature control |
Value of 0 = no ligatures, 1 = default ligatures, 2 = all ligatures. Default value 1. |
\expansionN |
Expansion factor to be applied to glyphs |
2000 * value of Default value 0. |
\obliquenessN |
Skew to be applied to glyphs |
2000 * value of 0 = no skew. Default value 0. |
\fsmilliN |
A finer specification for font size |
1000 * font size. Written in addition to \fs when \fs is not an integral or half-point value; value is overridden by \fs, so this should be written immediately after \fs. Default driven by \fs. |
\shadxN \shadyN |
Shadow offset, written in conjunction with \shad |
X and Y offsets in twips (0 = no offset). Defaults are \shadx3 and \shady-3. |
\shadrN |
Shadow blur, written in conjunction with \shad |
Blur radius in twips. 0 = no blur. Default value 0. |
\strikecN |
Strikethrough color |
Color number. Default same as foreground text color. |
\strikestyleN |
Strikethrough style, written where \strike, \striked, \strikew are not sufficient |
Style and pattern mask, value of 0 = none; 0x8000 = by word; styles: 1 = single, 2 = thick, 9 = double; patterns: 0x100 = dotted, 0x200 = dash, 0x300 = dash dot, 0x400 = dash dot dot. Default value 0. |
\strokecN |
Stroke color |
Color number. Default same as foreground text color. |
\strokewidthN |
Glyph stroke width, written in conjunction with \outl. |
20 * stroke width as percentage of font point size. 0 = no stroke. Default value 0. Negative values indicate that glyphs are both stroked and filled; the stroke width is taken from the absolute value of the parameter. |
\ulstyleN |
Underline style, written where the standard \ul commands are not sufficient |
Style and pattern mask, value of 0 = none; 0x8000 = by word; styles: 1 = single, 2 = thick, 9 = double; patterns: 0x100 = dotted, 0x200 = dash, 0x300 = dash dot, 0x400 = dash dot dot. Default value 0. |
{{\NeXTGraphic attachment \widthN \heightN} string} |
Name of attachment file in the same folder as the RTF file (typically packaged within an RTFD document) |
The attachment is the attachment file name, encoded in UTF-8 and properly RTF-escaped. The width and height parameters optionally specify the attachment size in twips. The string is always 0xAC. |
{{}{\*\glidN basestring}string} |
Glyph ID for explicitly specified glyphs. (The extra {} pair is necessary to work around an RTF reader bug in Mac OS X version 10.2 and earlier.) |
Glyph identifier (parameter to \glid). The basestring is the string the glyph id is intended to override; this attribute is then applied to the specified string. Typically string and basestring are the same, although string might contain multiple instances of basestring. |
{{}{\*\glidN basestring\glcolN} string} |
Glyph ID for explicitly specified glyphs |
Character identifier (parameter to \glid) and character collection (parameter to \glcol). Collection IDs: 0 = identity, 1 = Adobe-CNS1, 2 = Adobe-GB1, 3 = Adobe-Japan1, 4 = Adobe-Japan2, 5 = Adobe-Korea. |
{{}{\*\glid basestring\glnam glyphname}string} |
Glyph ID for explicitly specified glyphs |
The glyphname is the glyph name in UTF-8 encoding. |
\AppleTypeServicesUN |
Character shape control |
Value of The value is interpreted as Apple Type Services kCharacterShapeType selector + 1. The value 0 disables this attribute. Default value 0. |
Table 3 lists Apple’s RTF extensions for paragraph attributes.
Table 4 lists Apple’s RTF extensions for document attributes.
© 1997, 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-06-04)