- Inherits from:
- NSObject
- Package:
- com.apple.yellow.foundation
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 cluster's two public classes, NSAttributedString and NSMutableAttributedString, declare the programmatic interface for read-only attributed strings and modifiable attributed strings, respectively. The Application Kit uses a subclass of NSMutableAttributedString, called NSTextStorage, to provide the storage for the Application Kit's extended text-handling system.
NSAttributedString is not a subclass of String. It contains a string object to which it applies attributes. This protects users of attributed strings from ambiguities caused by the semantic differences between simple and attributed string. For example, equality can't be simply defined between a String and an attributed string.
You create an NSAttributedString object by using one of the Constructors. These methods initialize an attributed string with data you provide. The Application Kit also provides methods for creating attributed strings from RTF data.
An attributed string provides basic access to its contents with the stringReference and attributesAtIndex methods, which yield characters and attributes, respectively. These two primitive methods are used by the other access methods to retrieve attributes individually by name, by functional group (font or ruler attributes, for example), and so on.
The attribute values assigned to an attributed string become the property of that string, and shouldn't be modified in any way by other objects. Doing so can render inconsistent the attributed string's internal state. Always use NSMutableAttributedString's setAttributesInRange and related methods to change attribute values.
The NSAttributedString class declares the programmatic interface to an object that manages an immutable attributed string. NSAttributedString's two primitive methods provide the basis for all the other methods in the class. The primitive stringReference method returns the NSStringReference object to which attributes are applied. The primitive attributesAtIndex method returns an NSDictionary containing the attribute names and values for characters around a specified index.
NSAttributedString provides the following constants as a convenience:
Constant | Type | Description |
AttachmentAttributeName |
String | NSAttachment |
BackgroundColorAttributeName |
String | NSBackgroundColor |
BaselineOffsetAttributeName |
String | NSBaselineOffset |
FontAttributeName |
String | NSFont |
ForegroundColorAttributeName |
String | NSColor |
KernAttributeName |
String | NSKern |
LigatureAttributeName |
String | NSLigature |
LinkAttributeName |
String | NSLink |
NoUnderlineStyle |
int | 0 |
ParagraphStyleAttributeName |
String | NSParagraphStyle |
SingleUnderlineStyle |
int | 1 |
SuperscriptAttributeName |
String | NSSuperScript |
UnderlineByWordMask |
int | 32768 |
UnderlineStrikethroughMask |
int | 16384 |
UnderlineStyleAttributeName |
String | NSUnderline |
- Constructors
- NSAttributedString
- Retrieving character information
- stringReference
- length
- Retrieving attribute information
- attributesAtIndex
- attributeAtIndex
- Comparing attributed strings
- isEqualToAttributedString
- Extracting a substring
- attributedSubstringWithRange
public NSAttributedString()
public NSAttributedString(String aString)
public NSAttributedString(
String aString,
NSDictionary aNSDictionary)
public NSAttributedString(NSAttributedString aNSAttributedString)
public NSAttributedString(
URL anURL,
NSMutableDictionary aNSMutableDictionary)
public NSAttributedString(
NSData aNSData,
NSMutableDictionary aNSMutableDictionary)
public NSAttributedString(
Object anObject,
NSMutableDictionary aNSMutableDictionary)
public NSAttributedString(
NSData aNSData,
Object anObject,
NSMutableDictionary aNSMutableDictionary)
public Object RTFDFileWrapperFromRange(
NSRange aNSRange,
NSDictionary aNSDictionary)
public NSData RTFFromRange(
NSRange aNSRange,
NSDictionary aNSDictionary)
public Object attributeAtIndex(
String attributeName,
int index,
NSMutableRange aRange)
null
if
there is no such attribute. 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.
public Object attributeAtIndex(
String attributeName,
int index,
NSMutableRange aRange,
NSRange rangeLimit)
null
if
there is no such attribute. 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.
See Also: attributesAtIndex
public NSAttributedString attributedSubstringWithRange(NSRange aRange)
public NSDictionary attributesAtIndex(
int index,
NSMutableRange aRange)
null
it's filled with the range over
which the attributes and values apply. 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.
public NSDictionary attributesAtIndex(
int index,
NSMutableRange aRange,
NSRange rangeLimit)
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.
See Also: attributeAtIndex
public boolean containsAttachments()
public NSRange doubleClickAtIndex(int anInt)
public NSDictionary fontAttributesInRange(NSRange aNSRange)
public boolean isEqualToAttributedString(NSAttributedString otherString)
public int length()
See Also: length (NSStringReference)
public int lineBreakBeforeIndex(
int anInt,
NSRange aNSRange)
public int nextWordFromIndex(
int anInt,
boolean aBoolean)
public NSDictionary rulerAttributesInRange(NSRange aNSRange)
public NSStringReference stringReference()
This primitive method must guarantee efficient access to an attributed string's characters; subclasses should implement it to execute in O(1) time.