One Logo Pool Ball
Foundation · IOS API REFERENCE

AttributedSubstring

struct · iOS 15.0+ · SDK 27.1

@dynamicMemberLookup struct AttributedSubstring

A portion of an attributed string. ``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: ```swift // 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 ```

Members (13)

get · iOS 15.0+

characters

TypeScript
get characters(): AttributedString_CharacterView
Swift declaration
var characters: AttributedString.CharacterView { get }

The characters of the attributed string, as a view into the underlying string.

get · iOS 15.0+

startIndex

TypeScript
get startIndex(): AttributedString_Index
Swift declaration
var startIndex: AttributedString.Index { get }

The position of the first character in a nonempty attributed substring.

get · iOS 15.0+

description

TypeScript
get description(): string
Swift declaration
var description: String { get }

A textual representation of this instance. Calling this property directly is discouraged. Instead, convert an instance of any type to a string by using the `String(describing:)` initializer. This initializer works with any type, and uses the custom `description` property for types that conform to `CustomStringConvertible`: struct Point: CustomStringConvertible { let x: Int, y: Int var description: String { return "(\(x), \(y))" } } let p = Point(x: 21, y: 30) let s = String(describing: p) print(s) // Prints "(21, 30)" The conversion of `p` to a string in the assignment to `s` uses the `Point` type's `description` property.

method · iOS 15.0+

setAttributes

TypeScript
setAttributes(attributes: AttributeContainer): void
Swift declaration
mutating func setAttributes(_ attributes: AttributeContainer)

Sets the attributed substring's attributes to those in a specified attribute container. - Parameter attributes: The attribute container with the attributes to apply.

get · iOS 15.0+

unicodeScalars

TypeScript
get unicodeScalars(): AttributedString_UnicodeScalarView
Swift declaration
var unicodeScalars: AttributedString.UnicodeScalarView { get }

The Unicode scalars of the attributed string, as a view into the underlying string.

method · iOS 15.0+

mergeAttributes

TypeScript
mergeAttributes(attributes: AttributeContainer, mergePolicy?: AttributedString_AttributeMergePolicy): void
Swift declaration
mutating func mergeAttributes(_ attributes: AttributeContainer, mergePolicy: AttributedString.AttributeMergePolicy = .keepNew)

Merges the attributed string's attributes with those in a specified attribute container. - Parameters: - attributes: The attribute container with the attributes to merge. - mergePolicy: A policy to use when resolving conflicts.

method · iOS 15.0+

replaceAttributes

TypeScript
replaceAttributes(attributes: AttributeContainer, others: AttributeContainer): void
Swift declaration
mutating func replaceAttributes(_ attributes: AttributeContainer, with others: AttributeContainer)

Replaces the attributed substring's attributes with those in a specified attribute container. - Parameters: - attributes: The existing attributes to replace. - others: The new attributes to apply.

get · iOS 15.0+

base

TypeScript
get base(): AttributedString
Swift declaration
var base: AttributedString { get }

Returns the underlying attributed string that the attributed substring derives from.

get · iOS 15.0+

runs

TypeScript
get runs(): AttributedString_Runs
Swift declaration
var runs: AttributedString.Runs { get }

The attributed runs of the attributed string, as a view into the underlying string.

get · iOS 26.0+

utf8

TypeScript
get utf8(): AttributedString_UTF8View
Swift declaration
var utf8: AttributedString.UTF8View { get }

A view of the attributed substring's contents as a collection of UTF-8 code units.

get · iOS 26.0+

utf16

TypeScript
get utf16(): AttributedString_UTF16View
Swift declaration
var utf16: AttributedString.UTF16View { get }

A view of the attributed substring's contents as a collection of UTF-16 code units.

get · iOS 15.0+

endIndex

TypeScript
get endIndex(): AttributedString_Index
Swift declaration
var endIndex: AttributedString.Index { get }

A substring's past-the-end position -- the position one greater than the last valid subscript argument.

init · iOS 15.0+

init

TypeScript
static init(): AttributedSubstring
Swift declaration
init()

Creates an empty attributed substring.

Skipped declarations

4 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.

AttributedSubstring.subscript(_:)

This parameter hides a concrete Swift type.

AttributedSubstring.subscript(_:)

Passing a Swift type itself is not supported.

AttributedSubstring.subscript(dynamicMember:)

This declaration needs Swift generic type arguments. (no canonical witness for K)

AttributedSubstring.subscript(dynamicMember:)

Passing a Swift type itself is not supported.

All Foundation types →