Skip to content

Structure

AttributedSubstring

iOS 15.0+ iPadOS 15.0+ Mac Catalyst 15.0+ macOS 12.0+ tvOS 15.0+ watchOS 8.0+
A portion of an attributed string.
@dynamicMemberLookup struct AttributedSubstring

Overview

AttributedSubstring provides no-copy access to the contents of the string within the relevant range. The distinction between an AttributedString and an AttributedSubstring lets you distinguish between whether you’re in possession of an entire string or just a slice of it.

Because AttributedSubstring and AttributedString both conform to AttributedStringProtocol, working with ranges of AttributedString is natural. Modifying attributes by range works the same as it does on the base string.

If you use an AttributedSubstring to mutate its base AttributedString, you must perform your mutation inline, as the following example shows:

// Correct use of copying.
attrStr[range].link = url

// Incorrect use of AttributedString copy. Copies the referenced range of the base
// AttributedString and mutates that.
var substr = attrStr[range]
substr.link = url

Topics

Initializers

  • init()
    Creates an empty attributed substring.

Instance Properties

Default Implementations

Relationships

Conforms To