| Inherits from | |
| Framework | /System/Library/Frameworks/AppKit.framework |
| Companion guide | |
| Declared in | NSAttributedString.h NSStringDrawing.h NSTextAttachment.h |
The Application Kit extends Foundation’s NSAttributedString class by adding support for RTF (with or without attachments), graphics attributes (including font and ruler attributes), methods for drawing attributed strings, and methods for calculating significant linguistic units.
+ attributedStringWithAttachment:
– initWithData:options:documentAttributes:error:
– initWithDocFormat:documentAttributes:
– initWithHTML:documentAttributes:
– initWithHTML:baseURL:documentAttributes:
– initWithHTML:options:documentAttributes:
– initWithPath:documentAttributes:
– initWithRTF:documentAttributes:
– initWithRTFD:documentAttributes:
– initWithRTFDFileWrapper:documentAttributes:
– initWithURL:documentAttributes:
– initWithURL:options:documentAttributes:error:
– URLAtIndex:effectiveRange:
– doubleClickAtIndex:
– lineBreakBeforeIndex:withinRange:
– lineBreakByHyphenatingBeforeIndex:withinRange:
– nextWordFromIndex:forward:
– itemNumberInTextList:atIndex:
– rangeOfTextBlock:atIndex:
– rangeOfTextList:atIndex:
– rangeOfTextTable:atIndex:
– dataFromRange:documentAttributes:error:
– fileWrapperFromRange:documentAttributes:error:
– docFormatFromRange:documentAttributes:
– RTFFromRange:documentAttributes:
– RTFDFromRange:documentAttributes:
– RTFDFileWrapperFromRange:documentAttributes:
+ textFileTypes Deprecated in Mac OS X v10.5
+ textPasteboardTypes Deprecated in Mac OS X v10.5
+ textUnfilteredFileTypes Deprecated in Mac OS X v10.5
+ textUnfilteredPasteboardTypes Deprecated in Mac OS X v10.5
Creates an attributed string with an attachment.
+ (NSAttributedString *)attributedStringWithAttachment:(NSTextAttachment *)attachment
NSTextAttachment.hReturns an array of UTI strings identifying the file types supported by the receiver, either directly or through a user-installed filter service.
+ (NSArray *)textTypes
An array of NSString objects, each of which contains a UTI identifying a supported file type.
The returned list includes UTIs all file types supported by the receiver plus those that can be opened by the receiver after being converted by a user-installed filter service. You can use the returned UTI strings with any method that supports UTIs.
NSAttributedString.hReturns an array of UTI strings identifying the file types supported directly by the receiver.
+ (NSArray *)textUnfilteredTypes
An array of NSString objects, each of which contains a UTI identifying a supported file type.
The returned list includes UTI strings only for those file types that are supported directly by the receiver. It does not include types that are supported through user-installed filter services. You can use the returned UTI strings with any method that supports UTIs.
NSAttributedString.hCalculates and returns bounding rectangle for the receiver drawn using the options specified, within the given rectangle in the current graphics context.
- (NSRect)boundingRectWithSize:(NSSize)size options:(NSStringDrawingOptions)options
The origin of the rectangle returned from this method is the first glyph origin.
The values of NSStringDrawingOptions are listed in the “Constants” section of NSString Additions.
NSStringDrawing.hReturns YES if the receiver contains any attachment attributes, NO otherwise.
- (BOOL)containsAttachments
This method checks only for attachment attributes, not for NSAttachmentCharacter.
NSAttributedString.hReturns an NSData object that contains a text stream corresponding to the characters and attributes within the given range.
- (NSData *)dataFromRange:(NSRange)range documentAttributes:(NSDictionary *)dict error:(NSError **)error
Requires a document attributes dictionary dict specifying at least the NSDocumentTypeDocumentAttribute to determine the format to write. Raises an NSRangeException if any part of range lies beyond the end of the receiver’s characters. If unsuccessful, returns nil after setting error to point to an NSError object that encapsulates the reason why the object could not be created.
NSAttributedString.hReturns an NSData object that contains a Microsoft Word–format stream corresponding to the characters and attributes within the specified range.
- (NSData *)docFormatFromRange:(NSRange)range documentAttributes:(NSDictionary *)dict
The range is passed in the range parameter. Also writes the document-level attributes in dict, as explained in “Constants.” If there are no document-level attributes, dict can be nil. Raises an NSRangeException if any part of range lies beyond the end of the receiver’s characters.
NSAttributedString.hReturns the range of characters that form a word (or other linguistic unit) surrounding the given index, taking language characteristics into account.
- (NSRange)doubleClickAtIndex:(NSUInteger)index
Raises an NSRangeException if index lies beyond the end of the receiver’s characters.
NSAttributedString.hDraws the receiver with its font and other display attributes at the given point in the currently focused NSView.
- (void)drawAtPoint:(NSPoint)point
The width (height for vertical layout) of the rendering area is unlimited, unlike drawInRect:, which uses a bounding rectangle. As a result, this method renders the text in a single line.
Don’t invoke this method while no NSView is focused.
– lockFocus (NSView)– size– drawInRect:NSStringDrawing.hDraws the receiver with its font and other display attributes within the given rectangle in the currently focused NSView, clipping the text layout to this rectangle.
- (void)drawInRect:(NSRect)rect
Text is drawn within rect according to its line sweep direction; for example, Arabic text will begin at the right edge and potentially be clipped on the left.
The rect parameter determines how many glyphs are typeset within the width of a line, but it’s possible for a portion of a glyph to appear outside the area of rect if the image bounding box of the particular glyph exceeds its typographic bounding box.
If the focus view is flipped, the text origin is set at the upper-left corner of the drawing bounding box; otherwise the origin is set at the lower-left corner. For text rendering, whether the view coordinates are flipped or not doesn't affect the flow of line layout, which goes from top to bottom. However, it affects the interpretation of the text origin. So, for example, if the rect argument is {0.0, 0.0, 100.0, 100.0}, the text origin is {0.0, 0.0} when the view coordinates are flipped and {0.0, 100.0} when not.
Don’t invoke this method while no NSView is focused.
– lockFocus (NSView)– drawAtPoint:NSStringDrawing.hDraws the receiver with the specified options, within the given rectangle in the current graphics context.
- (void)drawWithRect:(NSRect)rect options:(NSStringDrawingOptions)options
The rect argument's origin field specifies the rendering origin. The point is interpreted as the baseline origin by default. With NSStringDrawingUsesLineFragmentOrigin, it is interpreted as the upper left corner of the line fragment rect. The size field specifies the text container size. The width part of the size field specifies the maximum line fragment width if larger than 0.0. The height defines the maximum size that can be occupied with text if larger than 0.0 and NSStringDrawingUsesLineFragmentOrigin is specified. If NSStringDrawingUsesLineFragmentOrigin is not specified, height is ignored and considered to be single-line rendering (NSLineBreakByWordWrapping and NSLineBreakByCharWrapping are treated as NSLineBreakByClipping).
The values of NSStringDrawingOptions are listed in the “Constants” section of NSString Additions.
You should only invoke this method when there is a current graphics context.
– drawAtPoint: (NSView)– lockFocusNSStringDrawing.hReturns an NSFileWrapper object that contains a text stream corresponding to the characters and attributes within the given range.
- (NSFileWrapper *)fileWrapperFromRange:(NSRange)range documentAttributes:(NSDictionary *)dict error:(NSError **)error
Requires a document attributes dictionary dict specifying at least the NSDocumentTypeDocumentAttribute to determine the format to write. Raises an NSRangeException if any part of range lies beyond the end of the receiver’s characters. Returns a directory file wrapper for those document types for which it is appropriate; otherwise a regular file wrapper. If unsuccessful, returns nil after setting error to point to an NSError object that encapsulates the reason why the object could not be created.
NSAttributedString.hReturns the font attributes in effect for the character at the given location.
- (NSDictionary *)fontAttributesInRange:(NSRange)aRange
Returns the font attributes in effect for the character at aRange.location. Font attributes are all those listed in “Standard Attributes”, except NSLinkAttributeName, NSParagraphStyleAttributeName, and NSAttachmentAttributeName. Use this method to obtain font attributes that are to be copied or pasted with “copy font” operations. Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.
NSAttributedString.hInitializes and returns a new NSAttributedString object from the data contained in the given NSData object.
- (id)initWithData:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
The options dictionary can contain the values described in “Option keys for importing documents” to specify how the document should be loaded. If NSDocumentTypeDocumentOption is specified, the document is treated as being in the specified format. If NSDocumentTypeDocumentOption is not specified, the method examines the document and loads it using whatever format it seems to contain. Also returns by reference in dict a dictionary containing document-level attributes described in “Constants.” The dict parameter may be nil, in which case no document attributes are returned. Returns nil if data can’t be decoded, after setting error to point to an NSError that encapsulates the reason why the attributed string object could not be created.
NSAttributedString.hInitializes and returns a new NSAttributedString object from Microsoft Word format data contained in the given NSData object.
- (id)initWithDocFormat:(NSData *)data documentAttributes:(NSDictionary **)docAttributes
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns nil if data can’t be decoded.
NSAttributedString.hInitializes and returns a new NSAttributedString object from the HTML contained in the given object and base URL.
- (id)initWithHTML:(NSData *)data baseURL:(NSURL *)aURL documentAttributes:(NSDictionary **)docAttributes
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns an initialized object, or nil if the file at aURL can’t be decoded.
NSAttributedString.hInitializes and returns a new NSAttributedString object from HTML contained in the given data object.
- (id)initWithHTML:(NSData *)data documentAttributes:(NSDictionary **)docAttributes
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns nil if data can’t be decoded.
NSAttributedString.hInitializes and returns a new NSAttributedString object from HTML contained in the given data object.
- (id)initWithHTML:(NSData *)data options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict
The options dictionary can contain the values described in “Option keys for importing documents.”
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns nil if data can’t be decoded.
NSAttributedString.hInitializes a new NSAttributedString object from RTF or RTFD data contained in the file at the given path.
- (id)initWithPath:(NSString *)path documentAttributes:(NSDictionary **)docAttributes
The contents of path will be examined to best load the file in whatever format it’s in. Filter services can be used to convert the file into a format recognized by Cocoa. Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns an initialized object, or nil if the file at path can’t be decoded.
NSAttributedString.hInitializes a new NSAttributedString object by decoding the stream of RTF commands and data contained in the given data object.
- (id)initWithRTF:(NSData *)rtfData documentAttributes:(NSDictionary **)docAttributes
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns an initialized object, or nil if rtfData can’t be decoded.
NSAttributedString.hInitializes a new NSAttributedString object by decoding the stream of RTFD commands and data contained in the given data object.
- (id)initWithRTFD:(NSData *)rtfdData documentAttributes:(NSDictionary **)docAttributes
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns an initialized object, or nil if rtfData can’t be decoded.
NSAttributedString.hInitializes a new NSAttributedString object from the given NSFileWrapper object containing an RTFD document.
- (id)initWithRTFDFileWrapper:(NSFileWrapper *)wrapper documentAttributes:(NSDictionary **)docAttributes
Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns an initialized object, or nil if wrapper can’t be interpreted as an RTFD document.
NSAttributedString.hInitializes a new NSAttributedString object from the data at the given URL.
- (id)initWithURL:(NSURL *)aURL documentAttributes:(NSDictionary **)docAttributes
The contents of aURL are examined to best load the file in whatever format it’s in. Filter services can be used to convert the file into a format recognized by Cocoa. Also returns by reference in docAttributes a dictionary containing document-level attributes described in “Constants.” docAttributes may be NULL, in which case no document attributes are returned. Returns an initialized object, or nil if the file at path can’t be decoded.
NSAttributedString.hInitializes a new NSAttributedString object from the contents of the given URL.
- (id)initWithURL:(NSURL *)url options:(NSDictionary *)options documentAttributes:(NSDictionary **)dict error:(NSError **)error
Filter services can be used to convert the file into a format recognized by Cocoa. The options dictionary specifies how the document should be loaded and can contain the values described in “Option keys for importing documents.”
If NSDocumentTypeDocumentOption is specified, the document is treated as being in the specified format. If NSDocumentTypeDocumentOption is not specified, the method examines the document and loads it using whatever format it seems to contain.
Also returns by reference in dict a dictionary containing document-level attributes described in “Constants.” The dict parameter may be nil, in which case no document attributes are returned. Returns an initialized object, or nil if the file at url can’t be decoded, after setting error to point to an NSError object that encapsulates the reason why the attributed string object could not be created.
NSAttributedString.hReturns the range of the item at the given index within the given list.
- (NSInteger)itemNumberInTextList:(NSTextList *)list atIndex:(NSUInteger)location
NSAttributedString.hReturns the index of the closest character before the given index, and within the given range, that can be placed on a new line when laying out text.
- (NSUInteger)lineBreakBeforeIndex:(NSUInteger)index withinRange:(NSRange)aRange
In other words, finds the appropriate line break when the character at index won’t fit on the same line as the character at the beginning of aRange. Returns NSNotFound if no line break is possible before index.Raises an NSRangeException if index or any part of aRange lies beyond the end of the receiver’s characters.
NSAttributedString.hReturns the index of the closest character before the given index, and within the given range, that can be placed on a new line by hyphenating.
- (NSUInteger)lineBreakByHyphenatingBeforeIndex:(NSUInteger)location withinRange:(NSRange)aRange
In other words, during text layout, finds the appropriate line break by hyphenation (the character index at which the hyphen glyph should be inserted) when the character at index won’t fit on the same line as the character at the beginning of aRange. Returns NSNotFound if no line break by hyphenation is possible before index.Raises an NSRangeException if index or any part of aRange lies beyond the end of the receiver’s characters.
NSAttributedString.hReturns the index of the first character of the word after or before the given index.
- (NSUInteger)nextWordFromIndex:(NSUInteger)index forward:(BOOL)flag
If flag is YES, this is the first character after index that begins a word; if flag is NO, it’s the first character before index that begins a word, whether index is located within a word or not. If index lies at either end of the string and the search direction would progress past that end, it’s returned unchanged. This method is intended for moving the insertion point during editing, not for linguistic analysis or parsing of text.Raises an NSRangeException if index lies beyond the end of the receiver’s characters.
NSAttributedString.hReturns the range of the individual text block that contains the given location.
- (NSRange)rangeOfTextBlock:(NSTextBlock *)block atIndex:(NSUInteger)location
The individual text is given by block and contains location.
NSAttributedString.hReturns the range of the given text list that contains the given location.
- (NSRange)rangeOfTextList:(NSTextList *)list atIndex:(NSUInteger)location
Returns the range of the list that contains location.
NSAttributedString.hReturns the range of the given text table that contains the given location
- (NSRange)rangeOfTextTable:(NSTextTable *)table atIndex:(NSUInteger)location
Returns the range of the text table that contains location.
NSAttributedString.hReturns an NSFileWrapper object that contains an RTFD document corresponding to the characters and attributes within the given range.
- (NSFileWrapper *)RTFDFileWrapperFromRange:(NSRange)aRange documentAttributes:(NSDictionary *)docAttributes
The file wrapper also includes the document-level attributes in docAttributes, as explained in “RTF Files and Attributed Strings”. If there are no document-level attributes, docAttributes can be nil. Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.You can save the file wrapper using the NSFileWrapper method writeToFile:atomically:updateFilenames:.
NSAttributedString.hReturns an NSData object that contains an RTFD stream corresponding to the characters and attributes within aRange.
- (NSData *)RTFDFromRange:(NSRange)aRange documentAttributes:(NSDictionary *)docAttributes
Also writes the document-level attributes in docAttributes, as explained in “RTF Files and Attributed Strings”. If there are no document-level attributes, docAttributes can be nil. Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.
When writing data to the pasteboard, you can use the NSData object as the first argument to the NSPasteboard method setData:forType:, with a second argument of NSRTFDPboardType.
NSAttributedString.hReturns an NSData object that contains an RTF stream corresponding to the characters and attributes within the given range, omitting all attachment attributes.
- (NSData *)RTFFromRange:(NSRange)aRange documentAttributes:(NSDictionary *)docAttributes
Also writes the document-level attributes in docAttributes, as explained in “RTF Files and Attributed Strings”. If there are no document-level attributes, docAttributes can be nil. Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.When writing data to the pasteboard, you can use the NSData object as the first argument to the NSPasteboard method setData:forType:, with a second argument of NSRTFPboardType. Although this method strips attachments, it leaves the attachment characters in the text itself. The NSText method RTFFromRange:, on the other hand, does strip attachment characters when extracting RTF.
NSAttributedString.hReturns the ruler (paragraph) attributes in effect for the characters within the given range.
- (NSDictionary *)rulerAttributesInRange:(NSRange)aRange
The only ruler attribute currently defined is that named by NSParagraphStyleAttributeName. Use this method to obtain attributes that are to be copied or pasted with “copy ruler” operations. Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.
NSAttributedString.hReturns the bounding box of the marks that the receiver draws.
- (NSSize)size
NSStringDrawing.hReturns a URL, either from a link attribute or from text at the given location that appears to be a URL string, for use in automatic link detection.
- (NSURL *)URLAtIndex:(NSUInteger)location effectiveRange:(NSRangePointer)effectiveRange
The character index in the string at which the method checks for a link.
The actual range covered by the link attribute or URL string, or of non-URL text if no apparent URL is found.
The URL found at location.
NSAttributedString.hAttributed strings support the following standard attributes for text. If the key is not in the dictionary, then use the default values described below.
NSString *NSFontAttributeName; NSString *NSParagraphStyleAttributeName; NSString *NSForegroundColorAttributeName; NSString *NSUnderlineStyleAttributeName; NSString *NSSuperscriptAttributeName; NSString *NSBackgroundColorAttributeName; NSString *NSAttachmentAttributeName; NSString *NSLigatureAttributeName; NSString *NSBaselineOffsetAttributeName; NSString *NSKernAttributeName; NSString *NSLinkAttributeName; NSString *NSStrokeWidthAttributeName; NSString *NSStrokeColorAttributeName; NSString *NSUnderlineColorAttributeName; NSString *NSStrikethroughStyleAttributeName; NSString *NSStrikethroughColorAttributeName; NSString *NSShadowAttributeName; NSString *NSObliquenessAttributeName; NSString *NSExpansionAttributeName; NSString *NSCursorAttributeName; NSString *NSToolTipAttributeName; NSString *NSMarkedClauseSegmentAttributeName;
NSFontAttributeNameNSFont
Default Helvetica 12-point
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSParagraphStyleAttributeNameNSParagraphStyle
Default as returned by the NSParagraphStyle method defaultParagraphStyle
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSForegroundColorAttributeNameNSColor
Default blackColor
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSUnderlineStyleAttributeNameNSNumber containing integer
Default 0, no underline. See “Underlining Patterns”, “Underlining Styles”, and “Underline Masks” for mask values.
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSSuperscriptAttributeNameNSNumber containing integer
Default 0
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSBackgroundColorAttributeNameNSColor
Default nil, no background
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSAttachmentAttributeNameNSTextAttachment
Default nil, no attachment
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSLigatureAttributeNameNSNumber containing integer
Default 1, standard ligatures; 0, no ligatures; 2, all ligatures
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSBaselineOffsetAttributeNameNSNumber containing floating point value, as points offset from baseline
Default 0.0
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSKernAttributeNameNSNumber containing floating point value, as points by which to modify default kerning
Default nil, use default kerning specified in font file; 0.0, kerning off; non-zero, points by which to modify default kerning
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSLinkAttributeNameNSURL (preferred) or NSString
Default nil, no link
Available in Mac OS X v10.0 and later.
Declared in NSAttributedString.h
NSStrokeWidthAttributeNameNSNumber containing floating point value, as percent of font point size
Default 0, no stroke; positive, stroke alone; negative, stroke and fill (a typical value for outlined text would be 3.0)
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSStrokeColorAttributeNameNSColor
Default nil, same as foreground color
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSUnderlineColorAttributeNameNSColor
Default nil, same as foreground color
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSStrikethroughStyleAttributeNameNSNumber containing integer
Default 0, no strikethrough. See “Underlining Patterns”, “Underlining Styles”, and “Underline Masks” for mask values.
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSStrikethroughColorAttributeNameNSColor
Default nil, same as foreground color
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSShadowAttributeNameNSShadow
Default nil, no shadow
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSObliquenessAttributeNameNSNumber containing floating point value, as skew to be applied to glyphs
Default 0.0, no skew
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSExpansionAttributeNameNSNumber containing floating point value, as log of expansion factor to be applied to glyphs
Default 0.0, no expansion
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSCursorAttributeNameNSCursor
Default as returned by the NSCursor method IBeamCursor
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSToolTipAttributeNameNSString
Default nil, no tooltip
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSMarkedClauseSegmentAttributeNameNSNumber containing an integer, as an index in marked text indicating clause segments
Available in Mac OS X v10.5 and later.
Declared in NSAttributedString.h
NSAttributedString.hThese constants define underlining style values for NSUnderlineStyleAttributeName and NSStrikethroughStyleAttributeName.
enum {
NSUnderlineStyleNone = 0x00,
NSUnderlineStyleSingle = 0x01,
NSUnderlineStyleThick = 0x02,
NSUnderlineStyleDouble = 0x09
};
NSUnderlineStyleNoneDo not draw an underline.
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSUnderlineStyleSingleDraw an underline consisting of a single line.
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSUnderlineStyleThickDraw an underline consisting of a thick line.
Available in Mac OS X v10.3 and later.
Declared in NSAttributedString.h
NSUnderlineStyleDoubleDraw an underline consisting of a double line.
Available in Mac OS X v10.3 and la