The NSMutable​Attributed​String class declares additional methods for mutating the content of an attributed string. You can add and remove characters (raw strings) and attributes separately or together as attributed strings. See the class description for NSAttributed​String for more information about attributed strings.


NSMutable​Attributed​String adds two primitive methods to those of NSAttributed​String. These primitive methods provide the basis for all the other methods in its class. The primitive replace​Characters(in:​with:​) method replaces a range of characters with those from a string, leaving all attribute information outside that range intact. The primitive set​Attributes(_:​range:​) method sets attributes and values for a given range of characters, replacing any previous attributes and values for that range.

In macOS, AppKit also uses NSParagraph​Style and its subclass NSMutable​Paragraph​Style to encapsulate the paragraph or ruler attributes used by the NSAttributed​String classes.

Note that the default font for NSAttributed​String objects is Helvetica 12-point, which may differ from the macOS system font, so you may wish to create the string with non-default attributes suitable for your application using, for example, init(string:​attributes:​).

NSMutable​Attributed​String is “toll-free bridged” with its Core Foundation counterpart, CFMutable​Attributed​String. See Toll-Free Bridging for more information.


Retrieving character information

var mutable​String:​ NSMutable​String

The character contents of the receiver as an NSMutable​String object.

Changing characters

func replace​Characters(in:​ NSRange, with:​ String)

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

func delete​Characters(in:​ NSRange)

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

Changing attributes

func set​Attributes([String :​ Any]?, range:​ NSRange)

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

func add​Attribute(String, value:​ Any, range:​ NSRange)

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

func add​Attributes([String :​ Any] = [:​], range:​ NSRange)

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

func remove​Attribute(String, range:​ NSRange)

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

func apply​Font​Traits(NSFont​Trait​Mask, range:​ NSRange)

Applies the font attributes specified by mask to the characters in a​Range.

func set​Alignment(NSText​Alignment, range:​ NSRange)

Sets the alignment characteristic of the paragraph style attribute for the characters in a​Range to alignment.

func set​Base​Writing​Direction(NSWriting​Direction, range:​ NSRange)

Sets the base writing direction for the characters in range to writing​Direction.

func subscript​Range(NSRange)

Decrements the value of the superscript attribute for characters in a​Range by 1.

func superscript​Range(NSRange)

Increments the value of the superscript attribute for characters in a​Range by 1.

func unscript​Range(NSRange)

Removes the superscript attribute from the characters in a​Range.

Changing characters and attributes

func append(NSAttributed​String)

Adds the characters and attributes of a given attributed string to the end of the receiver.

func insert(NSAttributed​String, at:​ Int)

Inserts the characters and attributes of the given attributed string into the receiver at the given index.

func replace​Characters(in:​ NSRange, with:​ NSAttributed​String)

Replaces the characters and attributes in a given range with the characters and attributes of the given attributed string.

func set​Attributed​String(NSAttributed​String)

Replaces the receiver’s entire contents with the characters and attributes of the given attributed string.

Grouping changes

func begin​Editing()

Overridden by subclasses to buffer or optimize a series of changes to the receiver’s characters or attributes, until it receives a matching end​Editing() message, upon which it can consolidate changes and notify any observers that it has changed.

func end​Editing()

Overridden by subclasses to consolidate changes made since a previous begin​Editing() message and to notify any observers of the changes.

Updating attachment contents

func update​Attachments(from​Path:​ String)

Updates all attachments based on files contained in the RTFD file package at path.

Fixing attributes after changes

func fix​Attributes(in:​ NSRange)

Cleans up font, paragraph style, and attachment attributes within the given range.

func fix​Attachment​Attribute(in:​ NSRange)

Cleans up attachment attributes in a​Range, removing all attachment attributes assigned to characters other than NSAttachment​Character.

func fix​Font​Attribute(in:​ NSRange)

Fixes the font attribute in a​Range, assigning default fonts to characters with illegal fonts for their scripts and otherwise correcting font attribute assignments.

func fix​Paragraph​Style​Attribute(in:​ NSRange)

Fixes the paragraph style attributes in a​Range, assigning the first paragraph style attribute value in each paragraph to all characters of the paragraph.