Important: The information in this document is obsolete and should not be used for new development.
Inherits from | |
Implements | |
Package | com.apple.cocoa.foundation |
Companion guide |
NSAttributedString objects manage character strings and associated sets of attributes (for example, font and kerning) that apply to individual characters or ranges of characters in the string. An association of characters and their attributes is called an attributed string. The classes NSAttributedString and NSMutableAttributedString declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively. Methods supporting the drawing of NSAttributedStrings are found in the Application Kit class NSGraphics. The Application Kit uses a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the Application Kit’s extended text-handling system.
The Application Kit also uses NSParagraphStyle and its subclass NSMutableParagraphStyle to encapsulate the paragraph or ruler attributes used by the NSAttributedString classes.
The mutable subclass of NSAttributedString is NSMutableAttributedString.
Note that the default font for NSAttributedString objects is Helvetica 12-point, which differs from the Mac OS X system font Lucida Grande, so you may wish to create the string with non-default attributes suitable for your application.
containsAttachments
doubleClickAtIndex
fontAttributesInRange
lineBreakBeforeIndex
lineBreakByHyphenatingBeforeIndex
nextWordFromIndex
rulerAttributesInRange
Creates an empty NSAttributedString.
public NSAttributedString
()
Creates an NSAttributedString with the characters of aString and no attribute information.
public NSAttributedString
(String aString)
Creates an NSAttributedString with the characters of aString and the attributes of attributes.
public NSAttributedString
(String aString, NSDictionary attributes)
Creates an NSAttributedString with the characters and attributes of attributedString.
public NSAttributedString
(NSAttributedString attributedString)
Creates an NSAttributedString with the contents of aURL, returning document properties, which are described in “Constants,” in attributes.
public NSAttributedString
(java.net.URL aURL, NSMutableDictionary attributes)
Creates an NSAttributedString with the contents of aData, returning document properties, which are described in “Constants,” in attributes.
public NSAttributedString
(NSData aData, NSMutableDictionary attributes)
Creates an NSAttributedString from wrapper, an NSFileWrapper object containing an RTFD document.
public NSAttributedString
(NSFileWrapper wrapper, NSMutableDictionary attributes)
Also returns in attributes a dictionary containing document-level attributes described in “Constants.” Returns null
if wrapper can’t be interpreted as an RTFD document.
Creates an NSAttributedString from the HTML contained in data and base URL aURL.
public NSAttributedString
(NSData data, java.net.URL aURL, NSMutableDictionary attributes)
Also returns in attributes a dictionary containing document-level attributes described in “Constants.” Returns null
if the file at aURL can’t be decoded.
Creates an NSAttributedString from HTML contained in data.
public NSAttributedString
(NSData data, NSDictionary options, NSMutableDictionary attributes)
options can contain one of the values described in NSMutableAttributedString’s readFromURL
method. Also returns in attributes a dictionary containing document-level attributes described in “Constants.” Returns null
if data can’t be decoded.
Returns the value for the attribute named attributeName of the character at index, or null
if there is no such attribute.
public Object attributeAtIndex
(String attributeName, int index, NSMutableRange aRange)
If the named attribute exists at index and aRange is non-null
, it’s filled with a range over which the named attribute’s value applies. If the named attribute doesn’t exist at index and aRange is non-null
, aRange is filled instead with the range over which the attribute doesn’t exist. This range isn’t necessarily the maximum range covered by attributeName, and its extent is implementation-dependent.
Throws an exception if index lies beyond the end of the receiver’s characters.
Returns the value for the attribute named attributeName of the character at index, or null
if there is no such attribute.
public Object attributeAtIndex
(String attributeName, int index, NSMutableRange aRange, NSRange rangeLimit)
If the named attribute exists at index and aRange is non-null
, it’s filled with the full range over which the value of the named attribute is the same as that at index, clipped to rangeLimit. If the named attribute doesn’t exist at index and aRange is non-null
, aRange is filled instead with the full range over which the attribute doesn’t exist, clipped to rangeLimit.
Throws an exception if index or any part of rangeLimit lies beyond the end of the receiver’s characters.
Returns an NSAttributedString object consisting of the characters and attributes within aRange in the receiver.
public NSAttributedString attributedSubstringWithRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters. This method treats the length of the string as a valid range value that returns an empty string.
Returns the attributes for the character at index.
public NSDictionary attributesAtIndex
(int index, NSMutableRange aRange)
If aRange is non-null
it’s filled with the range over which the attributes and values are the same as those at index. This range isn’t necessarily the maximum range covered, and its extent is implementation-dependent.
Throws an exception if index lies beyond the end of the receiver’s characters.
Returns the attributes for the character at index.
public NSDictionary attributesAtIndex
(int index, NSMutableRange aRange, NSRange rangeLimit)
If aRange is non-null
, it’s filled with the maximum range over which the attributes and values are the same as those at index, clipped to rangeLimit.
Throws an exception if index or any part of rangeLimit lies beyond the end of the receiver’s characters.
Returns true
if the receiver contains any attachment attributes, false
otherwise.
public boolean containsAttachments
()
This method checks only for attachment attributes, not for NSAttachmentCharacter
.
Returns an NSData object that contains a Microsoft Word–format stream corresponding to the characters and attributes within the specified range.
public NSData docFormatFromRange
(NSRange range, 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 null
. Throws an NSRangeException
if any part of range lies beyond the end of the receiver’s characters.
Returns the range of characters that form a word (or other linguistic unit) surrounding index, taking language characteristics into account.
public NSRange doubleClickAtIndex
(int index)
Throws a RangeException
if index lies beyond the end of the receiver’s characters.
Returns the font attributes in effect for the character at aRange.location
.
public NSDictionary fontAttributesInRange
(NSRange aRange)
Use this method to obtain font attributes that are to be copied or pasted with “copy font” operations. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Returns true
if the receiver is equal to otherString.
public boolean isEqualToAttributedString
(NSAttributedString otherString)
Attributed strings must match in both characters and attributes to be equal.
Returns the length of the receiver’s string object.
public int length
()
length
(NSStringReference)Returns the index of the closest character before index and within aRange that can be placed on a new line when laying out text.
public int lineBreakBeforeIndex
(int index, 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 NSArray.NotFound
if no line break is possible before index.Throws a RangeException
if index or any part of aRange lies beyond the end of the receiver’s characters.
Returns the index of the closest character before index and within aRange that can be placed on a new line by hyphenating.
public int lineBreakByHyphenatingBeforeIndex
(int location, 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 NSArray.NotFound
if no line break by hyphenation is possible before index.Throws a RangeException
if index or any part of aRange lies beyond the end of the receiver’s characters.
Returns the index of the first character of the word after or before index.
public int nextWordFromIndex
(int index, boolean flag)
If flag is true
, this is the first character after index that begins a word; if flag is false
, 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.Throws a RangeException
if index lies beyond the end of the receiver’s characters.
Returns an object that contains an RTFD document corresponding to the characters and attributes within aRange.
public Object RTFDFileWrapperFromRange
(NSRange aRange, NSDictionary docAttributes)
The file wrapper also includes the document-level attributes in docAttributes. If there are no document-level attributes, docAttributes can be null
. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.You can save the file wrapper using NSFileWrapper’s writeToFile
.
Returns an NSData object that contains an RTF stream corresponding to the characters and attributes within aRange, omitting all attachment attributes.
public NSData RTFFromRange
(NSRange aRange, NSDictionary docAttributes)
Also writes the document-level attributes in docAttributes. If there are no document-level attributes, docAttributes can be null
. Throws a RangeException
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 NSPasteboard’s setDataForType
, with a second argument of NSPasteboard.RTFPboardType
.Although this method strips attachments, it leaves the attachment characters in the text itself. NSText’s RTFFromRange
, on the other hand, does strip attachment characters when extracting RTF.
Returns the ruler (paragraph) attributes in effect for the characters within aRange.
public 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. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Returns the character contents of the receiver as an NSStringReference object.
public NSStringReference stringReference
()
This method doesn’t strip out attachment characters; use NSText’s string
method to extract just the linguistically significant characters.
For performance reasons, this method returns the current backing store of the attributed string object. If you want to maintain a snapshot of this as you manipulate the returned string, you should make a copy of the appropriate substring.
This primitive method must guarantee efficient access to an attributed string’s characters; subclasses should implement it to execute in O(1) time.
NSAttributedString provides the following attribute name constants:
Attribute Identifier |
Value Class |
Default Value |
---|---|---|
NSTextAttachment |
None (no attachment) |
|
NSColor |
None (no background drawn) |
|
float |
0.0 |
|
NSCursor |
IBeamCursor |
|
float |
0 (no expansion) |
|
NSFont |
Helvetica 12-point |
|
NSColor |
Black |
|
float |
0.0 |
|
int |
1 (standard ligatures) |
|
float |
0 (no skew) |
|
NSParagraphStyle |
Object returned by NSParagraphStyle’s |
|
NSShadow |
|
|
NSColor |
|
|
int |
0 (no strikethrough) |
|
NSColor |
|
|
float |
0 (no stroke) |
|
int |
0 |
|
String |
|
|
NSColor |
|
|
int |
None (no underline) |
NSAttributedString provides the following constants to use when working with underlines:
The following constants previously used for underline style were deprecated in Mac OS X v10.3:
The constructors can return a dictionary with the following document-wide attributes:
The following values can be returned for the DocumentType
key in the document attributes dictionary:
DocumentType |
---|
© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)