<!--
{
  "availability" : [
    "iOS: 15.0.0 -",
    "iPadOS: 15.0.0 -",
    "macCatalyst: 15.0.0 -",
    "macOS: 12.0.0 -",
    "tvOS: 15.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 8.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/AttributedSubstring",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation19AttributedSubstringV"
  },
  "title" : "AttributedSubstring"
}
-->

# AttributedSubstring

A portion of an attributed string.

```
@dynamicMemberLookup struct AttributedSubstring
```

## Overview

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

Because [`AttributedSubstring`](/documentation/Foundation/AttributedSubstring) and [`AttributedString`](/documentation/Foundation/AttributedString) both conform to [`AttributedStringProtocol`](/documentation/Foundation/AttributedStringProtocol), working with ranges of [`AttributedString`](/documentation/Foundation/AttributedString) is natural. Modifying attributes by range works the same as it does on the base string.

If you use an [`AttributedSubstring`](/documentation/Foundation/AttributedSubstring) to mutate its base [`AttributedString`](/documentation/Foundation/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
```

## Topics

### Creating Attributed Substrings

[`init()`](/documentation/Foundation/AttributedSubstring/init())

Creates an empty attributed substring.

### Applying and Modifying Attributes

[`func setAttributes(AttributeContainer)`](/documentation/Foundation/AttributedSubstring/setAttributes(_:))

Sets the attributed substring’s attributes to those in a specified attribute container.

[`func mergeAttributes(AttributeContainer, mergePolicy: AttributedString.AttributeMergePolicy)`](/documentation/Foundation/AttributedSubstring/mergeAttributes(_:mergePolicy:))

Merges the attributed string’s attributes with those in a specified attribute container.

[`enum AttributeMergePolicy`](/documentation/Foundation/AttributedString/AttributeMergePolicy)

An enumeration of behaviors to apply when merging attributes.

[`func replaceAttributes(AttributeContainer, with: AttributeContainer)`](/documentation/Foundation/AttributedSubstring/replaceAttributes(_:with:))

Replaces the attributed substring’s attributes with those in a specified attribute container.

### Searching for a Substring

### Accessing a Range

[`subscript(some RangeExpression<AttributedString.Index>) -> AttributedSubstring`](/documentation/Foundation/AttributedSubstring/subscript(_:)-96fey)

Returns a substring of the attributed substring that a range indicates.

### Accessing Indices

[Accessing Indicies Within an Attributed Substring](/documentation/Foundation/accessing-indicies-within-an-attributed-substring)

### Accessing Views into the Attributed Substring

[`var characters: AttributedString.CharacterView`](/documentation/Foundation/AttributedSubstring/characters)

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

[`struct CharacterView`](/documentation/Foundation/AttributedString/CharacterView)

A view into the underlying storage of the attributed string, as Unicode characters.

[`var unicodeScalars: AttributedString.UnicodeScalarView`](/documentation/Foundation/AttributedSubstring/unicodeScalars)

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

[`struct UnicodeScalarView`](/documentation/Foundation/AttributedString/UnicodeScalarView)

A view into the underlying storage of the attributed string, as Unicode scalars.

[`var runs: AttributedString.Runs`](/documentation/Foundation/AttributedSubstring/runs)

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

[`struct Runs`](/documentation/Foundation/AttributedString/Runs-swift.struct)

An iterable view into segments of the attributed string, each of which indicates where a run of identical attributes begins or ends.

### Accessing the Underlying Attributed String

[`var base: AttributedString`](/documentation/Foundation/AttributedSubstring/base)

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

### Accessing Whole-Substring Attributes

[`subscript<K>(K.Type) -> K.Value?`](/documentation/Foundation/AttributedSubstring/subscript(_:)-2hp64)

Returns an attribute value that corresponds to an attributed string key.

[`subscript<K>(dynamicMember _: KeyPath<AttributeDynamicLookup, K>) -> K.Value?`](/documentation/Foundation/AttributedSubstring/subscript(dynamicMember:)-3o8o1)

Returns an attribute value that a key path indicates.

[`enum AttributeDynamicLookup`](/documentation/Foundation/AttributeDynamicLookup)

A type to support dynamic member lookup of attributes and containers.

[`subscript<S>(dynamicMember _: KeyPath<AttributeScopes, S.Type>) -> ScopedAttributeContainer<S>`](/documentation/Foundation/AttributedSubstring/subscript(dynamicMember:)-548k0)

Returns a scoped attribute container that a key path indicates.

[`struct ScopedAttributeContainer`](/documentation/Foundation/ScopedAttributeContainer)

An attribute container that allows dynamic member lookup of its contents within the specified attribute scope.

### Default Implementations

[AttributedStringAttributeMutation Implementations](/documentation/Foundation/AttributedSubstring/AttributedStringAttributeMutation-Implementations)

[AttributedStringProtocol Implementations](/documentation/Foundation/AttributedSubstring/AttributedStringProtocol-Implementations)

## Relationships

### Conforms To

[`Copyable`](/documentation/Swift/Copyable)

[`AttributedStringAttributeMutation`](/documentation/Foundation/AttributedStringAttributeMutation)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`Escapable`](/documentation/Swift/Escapable)

[`Hashable`](/documentation/Swift/Hashable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Sendable`](/documentation/Swift/Sendable)

[`AttributedStringProtocol`](/documentation/Foundation/AttributedStringProtocol)

[`Equatable`](/documentation/Swift/Equatable)

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)