<!--
{
  "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/AttributedStringProtocol",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "s:10Foundation24AttributedStringProtocolP"
  },
  "title" : "AttributedStringProtocol"
}
-->

# AttributedStringProtocol

A protocol that provides common functionality to attributed strings and attributed substrings.

```
@dynamicMemberLookup protocol AttributedStringProtocol : AttributedStringAttributeMutation, CustomStringConvertible, Hashable, Sendable
```

## Overview

Don’t declare new conformances to [`AttributedStringProtocol`](/documentation/Foundation/AttributedStringProtocol). Only the [`AttributedString`](/documentation/Foundation/AttributedString) and [`AttributedSubstring`](/documentation/Foundation/AttributedSubstring) types in the standard library are valid conforming types.

## Topics

### Applying Attributes

[`func settingAttributes(AttributeContainer) -> AttributedString`](/documentation/Foundation/AttributedStringProtocol/settingAttributes(_:))

Returns an attributed string by setting the attributed string’s attributes to those in a specified attribute container.

[`func mergingAttributes(AttributeContainer, mergePolicy: AttributedString.AttributeMergePolicy) -> AttributedString`](/documentation/Foundation/AttributedStringProtocol/mergingAttributes(_:mergePolicy:))

Returns an attributed string by merging 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 replacingAttributes(AttributeContainer, with: AttributeContainer) -> AttributedString`](/documentation/Foundation/AttributedStringProtocol/replacingAttributes(_:with:))

Returns an attributed string by replacing occurrences of attributes in one attribute container with those in another attribute container.

### Searching for a Substring

[`func range<T>(of: T, options: String.CompareOptions, locale: Locale?) -> Range<AttributedString.Index>?`](/documentation/Foundation/AttributedStringProtocol/range(of:options:locale:))

Returns the range of a substring in the attributed string, if it exists.

### Accessing a Range

[`subscript<R>(R) -> AttributedSubstring`](/documentation/Foundation/AttributedStringProtocol/subscript(_:)-109me)

Returns a substring of the attributed string using a range to indicate the substring bounds.

### Accessing Indices

[`var startIndex: AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/startIndex)

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

[`var endIndex: AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/endIndex)

A string’s past-the-end position — the position one greater than the last valid subscript argument.

[`func index(AttributedString.Index, offsetByCharacters: Int) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(_:offsetByCharacters:))

Returns the position of the character offset a given distance, measured in characters, from a given string index.

[`func index(AttributedString.Index, offsetByRuns: Int) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(_:offsetByRuns:))

Returns the position of the run offset a given number of runs from a given string index.

[`func index(AttributedString.Index, offsetByUnicodeScalars: Int) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(_:offsetByUnicodeScalars:))

Returns the position of the Unicode scalar offset a given distance, measured in Unicode scalars, from a given string index.

[`func index(afterCharacter: AttributedString.Index) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(afterCharacter:))

Returns the position of the character immediately after another charcter indicated by an index.

[`func index(afterRun: AttributedString.Index) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(afterRun:))

Returns the position of the run immediately after a run indicated by an index.

[`func index(afterUnicodeScalar: AttributedString.Index) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(afterUnicodeScalar:))

Returns the position of the Unicode scalar immediately after a Unicode scalar indicated by an index.

[`func index(beforeCharacter: AttributedString.Index) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(beforeCharacter:))

Returns the position of the character immediately before another charcter indicated by an index.

[`func index(beforeRun: AttributedString.Index) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(beforeRun:))

Returns the position of the run immediately before a run indicated by an index.

[`func index(beforeUnicodeScalar: AttributedString.Index) -> AttributedString.Index`](/documentation/Foundation/AttributedStringProtocol/index(beforeUnicodeScalar:))

Returns the position of the Unicode scalar immediately before a Unicode scalar indicated by an index.

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

A type that represents the position of a character or code unit within an attributed string.

### Accessing Views into the Attributed String

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

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

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

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

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

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

### Accessing Whole-String Attributes

[`subscript<K>(K.Type) -> K.Value?`](/documentation/Foundation/AttributedStringProtocol/subscript(_:)-4thnp)

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

[`subscript<K>(dynamicMember _: KeyPath<AttributeDynamicLookup, K>) -> K.Value?`](/documentation/Foundation/AttributedStringProtocol/subscript(dynamicMember:)-2wake)

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/AttributedStringProtocol/subscript(dynamicMember:)-55pcu)

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.

### Comparing Attributed Strings or Substrings

[`static func == <RHS>(Self, RHS) -> Bool`](/documentation/Foundation/AttributedStringProtocol/==(_:_:))

### Instance Properties

[`var utf16: AttributedString.UTF16View`](/documentation/Foundation/AttributedStringProtocol/utf16)

[`var utf8: AttributedString.UTF8View`](/documentation/Foundation/AttributedStringProtocol/utf8)

### Default Implementations

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

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

## Relationships

### Conforming Types

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

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

### Inherits From

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

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

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

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

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

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

---

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)