Instance Method

initWithData:options:documentAttributes:error:

Initializes and returns a new attributed string object from the data contained in the given data object.

Declaration

- (instancetype)initWithData:(NSData *)data options:(NSDictionary<NSAttributedStringDocumentReadingOptionKey, id> *)options documentAttributes:(NSDictionary<NSAttributedStringDocumentAttributeKey, id> * _Nullable *)dict error:(NSError * _Nullable *)error;

Parameters

data

The data from which to create the string.

options

Document attributes for interpreting the document contents. NSDocumentTypeDocumentAttribute, NSCharacterEncodingDocumentAttribute, and NSDefaultAttributesDocumentAttribute are supported option keys. If you pass an empty dictionary, the method examines the data to attempt to determine the appropriate attributes.

dict

An in-out dictionary containing document-level attributes described in Document Attributes. May be NULL, in which case no document attributes are returned.

error

An in-out variable containing an encountered error, if any.

Return Value

Returns an initialized attributed string object, or nil if the data can’t be decoded.

Discussion

The HTML importer should not be called from a background thread (that is, the options dictionary includes NSDocumentTypeDocumentAttribute with a value of NSHTMLTextDocumentType). It will try to synchronize with the main thread, fail, and time out. Calling it from the main thread works (but can still time out if the HTML contains references to external resources, which should be avoided at all costs). The HTML import mechanism is meant for implementing something like markdown (that is, text styles, colors, and so on), not for general HTML import.

See Also

Creating an NSAttributedString Object

- initWithString:

Returns an NSAttributedString object initialized with the characters of a given string and no attribute information.

- initWithString:attributes:

Returns an NSAttributedString object initialized with a given string and attributes.

- initWithAttributedString:

Returns an NSAttributedString object initialized with the characters and attributes of another given attributed string.

- initWithDocFormat:documentAttributes:

Initializes and returns a new NSAttributedString object from Microsoft Word format data contained in the given NSData object.

- initWithHTML:documentAttributes:

Initializes and returns a new NSAttributedString object from HTML contained in the given data object.

- initWithHTML:baseURL:documentAttributes:

Initializes and returns a new NSAttributedString object from the HTML contained in the given object and base URL.

- initWithHTML:options:documentAttributes:

Initializes and returns a new attributed string object from HTML contained in the given data object.

- initWithRTF:documentAttributes:

Initializes a new attributed string object by decoding the stream of RTF commands and data contained in the given data object.

- initWithRTFD:documentAttributes:

Initializes a new attributed string object by decoding the stream of RTFD commands and data contained in the given data object.

- initWithRTFDFileWrapper:documentAttributes:

Initializes a new attributed string object from the specified file wrapper containing an RTFD document.

- initWithURL:options:documentAttributes:error:

Initializes a new NSAttributedString object from the contents of the given URL.

+ attributedStringWithAttachment:

Creates an attributed string with an attachment.