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 |
NSMutableAttributedString declares the programmatic interface to objects that manage mutable attributed strings. You can add and remove characters (raw strings) and attributes separately or together as attributed strings. See the class description for NSAttributedString for more information about attributed strings.
When working with the Application Kit, you must also clean up changed attributes using the various fix...
methods.
NSMutableAttributedString adds two primitive methods to those of NSAttributedString. These primitive methods provide the basis for all the other methods in its class. The primitive replaceCharactersInRange
method replaces a range of characters with those from a string, leaving all attribute information outside that range intact. The primitive setAttributesInRange
method sets attributes and values for a given range of characters, replacing any previous attributes and values for that range.
The Application Kit also uses NSParagraphStyle and its subclass NSMutableParagraphStyle to encapsulate the paragraph or ruler attributes used by the NSAttributedString classes.
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.
appendAttributedString
applyFontTraitsInRange
fixAttachmentAttributeInRange
fixAttributesInRange
fixFontAttributeInRange
fixParagraphStyleAttributeInRange
insertAttributedStringAtIndex
readFromData
readFromURL
replaceCharactersInRange
setAlignmentInRange
setAttributedString
subscriptRange
superscriptRange
unscriptRange
updateAttachmentsFromPath
Creates an empty NSMutableAttributedString.
public NSMutableAttributedString
()
Creates an NSMutableAttributedString with the characters and attributes of attributedString.
public NSMutableAttributedString
(NSAttributedString attributedString)
Creates an NSMutableAttributedString with the contents of aData, returning document properties in attributes.
public NSMutableAttributedString
(NSData aData, NSMutableDictionary attributes)
Creates an NSMutableAttributedString from the HTML contained in data and base URL aURL.
public NSMutableAttributedString
(NSData data, java.net.url aURL, NSMutableDictionary attributes)
Also returns in attributes a dictionary containing document-level attributes described in NSAttributedString’s “Constants.” Returns null
if the file at aURL can’t be decoded.
Creates an NSMutableAttributedString from wrapper, an NSFileWrapper object containing an RTFD document.
public NSMutableAttributedString
(NSFileWrapper wrapper, NSMutableDictionary attributes)
Also returns in attributes a dictionary containing document-level attributes described in NSAttributedString’s “Constants.” Returns null
if wrapper can’t be interpreted as an RTFD document.
Creates an NSMutableAttributedString with the characters of string
public NSMutableAttributedString
(String string)
and no attribute information
Creates an NSMutableAttributedString with the characters of aString and the attributes of attributes.
public NSMutableAttributedString
(String aString, NSDictionary attributes)
Creates an NSMutableAttributedString with the contents of aURL, returning document properties, which are described in NSAttributedString’s “Constants,” in attributes.
public NSMutableAttributedString
(java.net.URL aURL, NSMutableDictionary attributes)
Creates an NSMutableAttributedString with the contents of aURL, returning document properties, which are described in NSAttributedString’s “Constants,” in attributes.
public NSMutableAttributedString
(NSData aURL, NSDictionary options, NSMutableDictionary attributes)
options can contain one of the values described in readFromURL
.
Adds an attribute with the given name and value to the characters in aRange.
public void addAttributeInRange
(String name, Object value, NSRange aRange)
Throws an InvalidArgumentException
if name or value is null
and a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Adds the collection of attributes in attributes to the characters in aRange.
public void addAttributesInRange
(NSDictionary attributes, NSRange aRange)
Throws an InvalidArgumentException
if attributes is null
and a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Adds the characters and attributes of attributedString to the end of the receiver.
public void appendAttributedString
(NSAttributedString attributedString)
Applies the font attributes specified by mask to the characters in aRange.
public void applyFontTraitsInRange
(int mask, NSRange aRange)
See the NSFontManager class specification for a description of the font traits available. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Overridden by subclasses to buffer or optimize a series of changes to the receiver’s characters or attributes, until it receives a matching endEditing
message, upon which it can consolidate changes and notify any observers that it has changed.
public void beginEditing
()
You can nest pairs of beginEditing
and endEditing
messages.
Deletes the characters in aRange along with their associated attributes.
public void deleteCharactersInRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Overridden by subclasses to consolidate changes made since a previous beginEditing
message and to notify any observers of the changes.
public void endEditing
()
NSMutableAttributedString’s implementation does nothing. NSTextStorage, for example, overrides this method to invoke fixAttributesInRange
and to inform its NSLayoutManagers that they need to re-lay the text.
processEditing
(NSTextStorage)Cleans up attachment attributes in aRange, removing all attachment attributes assigned to characters other than NSTextAttachment.AttachmentCharacter
.
public void fixAttachmentAttributeInRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Invokes the other fix...
methods, allowing you to clean up an attributed string with a single message.
public void fixAttributesInRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Fixes the font attribute in aRange, assigning default fonts to characters with illegal fonts for their scripts and otherwise correcting font attribute assignments.
public void fixFontAttributeInRange
(NSRange aRange)
For example, Kanji characters assigned a Latin font are reassigned an appropriate Kanji font. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Fixes the paragraph style attributes in aRange, assigning the first paragraph style attribute value in each paragraph to all characters of the paragraph.
public void fixParagraphStyleAttributeInRange
(NSRange aRange)
This method extends the range as needed to cover the last paragraph partially contained. A paragraph is delimited by any of these characters, the longest possible sequence being preferred to any shorter:
U+000D (\r
or CR)
U+000A (\n
or LF)
U+2028 (Unicode line separator)
U+2029 (Unicode paragraph separator) \r\n
, in that order (also known as CRLF)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Inserts the characters and attributes of attributedString into the receiver, so the new characters and attributes begin at index and the existing characters and attributes from index to the end are shifted by the length of attributedString.
public void insertAttributedStringAtIndex
(NSAttributedString attributedString, int index)
Throws a RangeException
if index lies beyond the end of the receiver’s characters.
Returns the character contents of the receiver as an NSMutableStringReference object.
public NSMutableStringReference mutableStringReference
()
The receiver tracks changes to this string and keeps its attribute mappings up to date.
public boolean readFromData
(NSData data, NSDictionary options, NSMutableDictionary dict)
Sets the contents of the receiver from the stream at data. options can contain one of the values described in readFromURL
.
On return, the documentAttributes dictionary (if provided) contains the various keys described in the “Constants” section of NSAttributedString.
Sets the contents of receiver from the file at url.
public boolean readFromURL
(java.net.URL url, NSDictionary options, NSMutableDictionary documentAttributes)
Filter services can be used to convert the contents of the URL into a format recognized by Cocoa. options can contain:
Key |
Description |
---|---|
|
For plain text documents; the unsigned int to be used if the encoding cannot be determined |
|
For HTML documents; java.net.URL containing base URL |
|
NSDictionary containing attributes to be applied to plain files |
Starting with Mac OS X v10.3, these options keys are recognized for HTML documents, both by this method as well as the NSAttributedString constructor with an options parameter:
Key |
Description |
---|---|
|
An integer. If present and positive, forces WebKit-based HTML importing be used; behavior in this case may differ from HTML import in Mac OS X v10.2 and before, particularly for tables. |
|
String containing the name, IANA or otherwise, of a text encoding to be used if the encoding cannot be determined from the document. Mutually exclusive with |
|
A float. Time in seconds to wait for a document to finish loading. |
|
WebPreferences. If WebKit-based HTML importing is used, specifies a WebPreferences object. If not present, a default set of preferences is used. |
|
NSObject. If WebKit-based HTML importing is used, specifies an object to serve as the WebResourceLoadDelegate. If not present, a default delegate will be used that will permit the loading of subsidiary resources but will not respond to authentication challenges. |
On return, the documentAttributes dictionary (if provided) contains the various keys described in the “Constants” section of NSAttributedString.
Removes the attribute named name from the characters in aRange.
public void removeAttributeInRange
(String name, NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Replaces the characters and attributes in aRange with the characters and attributes of attributedString.
public void replaceCharactersInRange
(NSRange aRange, NSAttributedString attributedString)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Replaces the characters in aRange with the characters of aString.
public void replaceCharactersInRange
(NSRange aRange, String aString)
The new characters inherit the attributes of the first replaced character from aRange. Where the length of aRange is 0, the new characters inherit the attributes of the character preceding aRange if it has any, otherwise of the character following aRange.
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Sets the alignment characteristic of the paragraph style attribute for the characters in aRange to alignment.
public void setAlignmentInRange
(int alignment, NSRange aRange)
When attribute fixing takes place, this change will affect only paragraphs whose first character was included in aRange. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Replaces the receiver’s entire contents with the characters and attributes of attributedString.
public void setAttributedString
(NSAttributedString attributedString)
Sets the attributes for the characters in aRange to attributes.
public void setAttributesInRange
(NSDictionary attributes, NSRange aRange)
These new attributes replace any attributes previously associated with the characters in aRange. Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
To set attributes for a zero-length NSMutableAttributedString displayed in a text view, use the NSTextView method setTypingAttributes
.
Decrements the value of the superscript attribute for characters in aRange by 1.
public void subscriptRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Increments the value of the superscript attribute for characters in aRange by 1.
public void superscriptRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Removes the superscript attribute from the characters in aRange.
public void unscriptRange
(NSRange aRange)
Throws a RangeException
if any part of aRange lies beyond the end of the receiver’s characters.
Updates all attachments based on files contained in the RTFD file package at path.
public void updateAttachmentsFromPath
(String path)
updateFromPath
(NSFileWrapper)© 1997, 2006 Apple Computer, Inc. All Rights Reserved. (Last updated: 2006-07-24)