<!--
{
  "availability" : [
    "iOS: 26.0.0 -",
    "iPadOS: 26.0.0 -",
    "macCatalyst: 26.0.0 -",
    "macOS: 26.0.0 -",
    "tvOS: 26.0.0 -",
    "visionOS: 26.0.0 -",
    "watchOS: 26.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/AttributedTextFormattingDefinition",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI34AttributedTextFormattingDefinitionP"
  },
  "title" : "AttributedTextFormattingDefinition"
}
-->

# AttributedTextFormattingDefinition

A protocol for defining how text can be styled in a view.

```
protocol AttributedTextFormattingDefinition<Scope>
```

## Overview

A formatting definition consists of an attribute scope and a number of
value constraints. It is applied to a view hierarchy using the
[`attributedTextFormattingDefinition(_:)`](/documentation/SwiftUI/View/attributedTextFormattingDefinition(_:)-81jn6) view modifier
and affects nested [`Text`](/documentation/SwiftUI/Text) and [`TextEditor`](/documentation/SwiftUI/TextEditor) views when initialized with
<doc://com.apple.documentation/documentation/Foundation/AttributedString>.

Create a formatting definition by first choosing an attribute scope that
contains all attributes relevant for your view. All other attributes will be
ignored by value constraints and by the affected views.

Use the `Foundation/AttributeScopes/SwiftUIAttributes` for the
default set of attributes supported by SwiftUI. You can create your own
scope only listing out a subset of the attributes in SwiftUI’s attribute
scope. You can also include custom attributes in your scope. This allows you
to take advantage of advanced attributed string features, such as
<doc://com.apple.documentation/documentation/Foundation/AttributedStringKey/runBoundaries>.

Custom attributes also allow you to separate semantic information stored on
the text, e.g. the information that a sequence of characters refers a
specific person in contacts, from how this part of the text is to be
formatted, e.g. with the foreground color “purple”. The rules defining what
values attributes can have, are called [`AttributedTextValueConstraint`](/documentation/SwiftUI/AttributedTextValueConstraint)s.

```
struct ContactsArePurple: AttributedTextValueConstraint {
    typealias Scope = MyScope
    typealias AttributeKey = Scope.ForegroundColorAttribute

    func constrain(_ container: inout Attributes) {
        if container.annotation == .contact {
            container.foregroundColor = .purple
        } else {
            container.foregroundColor = nil
        }
    }
}
```

While associating formatting with custom semantic attributes is one
important use case, value constraints are a generic mechanism for
constraining the formatting that is available in a text editor - with or
without dependencies on other attributes. For example, a value constraint
could also be used to only allow a single, solid underline, but not a double
underline or a dashed underline.

SwiftUI validates formatting UI provided by the system to the user to make
sure only controls that are compatible with your formatting definition and
its constraints are visible and enabled. If the system formatting UI does
not provide sufficient utility based on your formatting definition, or you
provide custom UI that is better tailored to your text editing experience,
consider hiding the system-provided UI using the
[`textInputFormattingControlVisibility(_:for:)`](/documentation/SwiftUI/View/textInputFormattingControlVisibility(_:for:)) view modifier.

To declare the attributed text formatting definition, specify the attribute
scope in the generic of the [`body`](/documentation/SwiftUI/AttributedTextFormattingDefinition/body-1b01t)’s type, and list all value constraints
inside the [`body`](/documentation/SwiftUI/AttributedTextFormattingDefinition/body-1b01t) using result builder syntax:

```
struct MyTextFormattingDefinition: AttributedTextFormattingDefinition {
    var body: some AttributedTextFormattingDefinition<
        AttributeScopes.SwiftUIAttributes
    > {
        ValueConstraint(
            for: \.underlineStyle,
            values: [nil, .single],
            default: .single)
        MyAttributedTextValueConstraint()
        ContactsArePurple()
    }
}
```

Use the [`attributedTextFormattingDefinition(_:)`](/documentation/SwiftUI/View/attributedTextFormattingDefinition(_:)-81jn6) view modifier to
apply the definition to a view.

## Topics

### Associated Types

[`associatedtype Body : AttributedTextFormattingDefinition`](/documentation/SwiftUI/AttributedTextFormattingDefinition/Body-swift.associatedtype)

The type of view representing the body of this formatting definition.

[`associatedtype Scope : AttributeScope`](/documentation/SwiftUI/AttributedTextFormattingDefinition/Scope)

The text formatting definition only allows usage of attributes in this
attribute scope.

### Instance Properties

[`var body: Self.Body`](/documentation/SwiftUI/AttributedTextFormattingDefinition/body-1b01t)

The constraints of the formatting definition.

### Instance Methods

[`func constrain(_:)`](/documentation/SwiftUI/AttributedTextFormattingDefinition/constrain(_:))

Applies all value constraints to a given attribute container.

### Type Aliases

[`typealias ValueConstraint`](/documentation/SwiftUI/AttributedTextFormattingDefinition/ValueConstraint)

A text formatting definition that permits a single attribute to be used,
optionally constrained to a set of values.

## Relationships

### Conforming Types

[`EmptyDefinition`](/documentation/SwiftUI/AttributedTextFormatting/EmptyDefinition)

[`AnyDefinition`](/documentation/SwiftUI/AttributedTextFormatting/AnyDefinition)

[`ValueConstraint`](/documentation/SwiftUI/AttributedTextFormatting/ValueConstraint)

[`TupleDefinition`](/documentation/SwiftUI/AttributedTextFormatting/TupleDefinition)

### Inherited By

[`AttributedTextValueConstraint`](/documentation/SwiftUI/AttributedTextValueConstraint)

---

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)