Contents

NSMutableAttributedString

A mutable string with associated attributes (such as visual style, hyperlinks, or accessibility data) for portions of its text.

Declaration

class NSMutableAttributedString

Overview

The NSMutableAttributedString 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 NSAttributedString for more information about attributed strings.

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 replaceCharacters(in:with:) method replaces a range of characters with those from a string, leaving all attribute information outside that range intact. The primitive setAttributes(_: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 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 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:).

NSMutableAttributedString is “toll-free bridged” with its Core Foundation counterpart, CFMutableAttributedString. See Toll-Free Bridging for more information.

Topics

Retrieving Character Information

Changing Characters

Changing Attributes

Changing Characters and Attributes

Grouping Changes

Updating Attachment Contents

Fixing Attributes After Changes

Reading Content

Deprecated

See Also

Strings with Metadata