Class

NSMutableAttributedString

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.

Overview

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.

Symbols

Retrieving character information

var mutableString: NSMutableString

The character contents of the receiver as an NSMutableString object.

Changing characters

func replaceCharacters(in: NSRange, with: String)

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

func deleteCharacters(in: NSRange)

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

Changing attributes

func setAttributes([String : Any]?, range: NSRange)

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

func addAttribute(String, value: Any, range: NSRange)

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

func addAttributes([String : Any] = [:], range: NSRange)

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

func removeAttribute(String, range: NSRange)

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

func applyFontTraits(NSFontTraitMask, range: NSRange)

Applies the font attributes specified by mask to the characters in aRange.

func setAlignment(NSTextAlignment, range: NSRange)

Sets the alignment characteristic of the paragraph style attribute for the characters in aRange to alignment.

func setBaseWritingDirection(NSWritingDirection, range: NSRange)

Sets the base writing direction for the characters in range to writingDirection.

func subscriptRange(NSRange)

Decrements the value of the superscript attribute for characters in aRange by 1.

func superscriptRange(NSRange)

Increments the value of the superscript attribute for characters in aRange by 1.

func unscriptRange(NSRange)

Removes the superscript attribute from the characters in aRange.

Changing characters and attributes

func append(NSAttributedString)

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

func insert(NSAttributedString, at: Int)

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

func replaceCharacters(in: NSRange, with: NSAttributedString)

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

func setAttributedString(NSAttributedString)

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

Grouping changes

func beginEditing()

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.

func endEditing()

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

Updating attachment contents

func updateAttachments(fromPath: String)

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

Fixing attributes after changes

func fixAttributes(in: NSRange)

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

func fixAttachmentAttribute(in: NSRange)

Cleans up attachment attributes in aRange, removing all attachment attributes assigned to characters other than NSAttachmentCharacter.

func fixFontAttribute(in: NSRange)

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

func fixParagraphStyleAttribute(in: NSRange)

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

Relationships

Inherits From

Conforms To