iOS Developer Library

Developer

Foundation Framework Reference NSMutableAttributedString Class Reference

Options
Deployment Target:

On This Page
Language:

NSMutableAttributedString

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. More...

Inheritance


Import Statement


import Foundation @import Foundation;

Availability


Available in iOS 3.2 and later.
  • The character contents of the receiver as an NSMutableString object.

    Declaration

    Swift

    var mutableString: NSMutableString { get }

    Objective-C

    @property(readonly, retain) NSMutableString *mutableString

    Discussion

    The receiver tracks changes to this string and keeps its attribute mappings up to date.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Replaces the characters in the given range with the characters of the given string.

    Declaration

    Swift

    func replaceCharactersInRange(_ aRange: NSRange, withString aString: String)

    Objective-C

    - (void)replaceCharactersInRange:(NSRange)aRange withString:(NSString *)aString

    Parameters

    aRange

    A range specifying the characters to replace.

    aString

    A string specifying the characters to replace those in aRange.

    Discussion

    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.

    Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Deletes the characters in the given range along with their associated attributes.

    Declaration

    Swift

    func deleteCharactersInRange(_ aRange: NSRange)

    Objective-C

    - (void)deleteCharactersInRange:(NSRange)aRange

    Parameters

    aRange

    A range specifying the characters to delete.

    Discussion

    Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Sets the attributes for the characters in the specified range to the specified attributes.

    Declaration

    Swift

    func setAttributes(_ attributes: [NSObject : AnyObject]?, range aRange: NSRange)

    Objective-C

    - (void)setAttributes:(NSDictionary *)attributes range:(NSRange)aRange

    Parameters

    attributes

    A dictionary containing the attributes to set. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

    aRange

    The range of characters whose attributes are set.

    Discussion

    These new attributes replace any attributes previously associated with the characters in aRange. Raises an NSRangeException 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:.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Adds an attribute with the given name and value to the characters in the specified range.

    Declaration

    Swift

    func addAttribute(_ name: String, value value: AnyObject, range aRange: NSRange)

    Objective-C

    - (void)addAttribute:(NSString *)name value:(id)value range:(NSRange)aRange

    Parameters

    name

    A string specifying the attribute name. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

    value

    The attribute value associated with name.

    aRange

    The range of characters to which the specified attribute/value pair applies.

    Discussion

    You may assign any name/value pair you wish to a range of characters, in addition to the standard attributes described in the Constants section of NSAttributedString Additions. Raises an NSInvalidArgumentException if name or value is nil and an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Adds the given collection of attributes to the characters in the specified range.

    Declaration

    Swift

    func addAttributes(_ attributes: [NSObject : AnyObject], range aRange: NSRange)

    Objective-C

    - (void)addAttributes:(NSDictionary *)attributes range:(NSRange)aRange

    Parameters

    attributes

    A dictionary containing the attributes to add. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

    aRange

    The range of characters to which the specified attributes apply.

    Discussion

    You may assign any name/value pair you wish to a range of characters, in addition to the standard attributes described in the Constants section of NSAttributedString Additions. Raises an NSInvalidArgumentException if attributes is nil and an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Removes the named attribute from the characters in the specified range.

    Declaration

    Swift

    func removeAttribute(_ name: String, range aRange: NSRange)

    Objective-C

    - (void)removeAttribute:(NSString *)name range:(NSRange)aRange

    Parameters

    name

    A string specifying the attribute name to remove. Attribute keys can be supplied by another framework or can be custom ones you define. For information about where to find the system-supplied attribute keys, see the overview section in NSAttributedString Class Reference.

    aRange

    The range of characters from which the specified attribute is removed.

    Discussion

    Raises an NSRangeException if any part of aRange lies beyond the end of the receiver’s characters.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • 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.

    Declaration

    Swift

    func beginEditing()

    Objective-C

    - (void)beginEditing

    Discussion

    You can nest pairs of beginEditing and endEditing messages.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

  • Overridden by subclasses to consolidate changes made since a previous beginEditing message and to notify any observers of the changes.

    Declaration

    Swift

    func endEditing()

    Objective-C

    - (void)endEditing

    Discussion

    The NSMutableAttributedString implementation does nothing. NSTextStorage, for example, overrides this method to invoke fixAttributesInRange: and to inform its NSLayoutManager objects that they need to re-lay the text.

    Import Statement

    import Foundation

    Availability

    Available in iOS 3.2 and later.

    See Also

    – processEditing (NSTextStorage)