<!--
{
  "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/View/attributedTextFormattingDefinition(_:)-81jn6",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE34attributedTextFormattingDefinitionyQrqd__AA010AttributedefG0Rd__lF"
  },
  "title" : "attributedTextFormattingDefinition(_:)"
}
-->

# attributedTextFormattingDefinition(_:)

Apply a text formatting definition to nested views.

```
nonisolated func attributedTextFormattingDefinition<D>(_ definition: D) -> some View where D : AttributedTextFormattingDefinition
```

## Discussion

Applying a text formatting definition to a `Text` or `TextEditor`
created using the `Text/init(_:)-(AttributedString)` or
`TextEditor/init(text:selection:)` initializer, respectively, makes
sure that any content observable to the user adheres to the constraints
of the formatting definition.

You can compose your own definition from an attribute scope and a series
of `AttributedTextValueConstraint`s:

```
// MyTextFormattingDefinition.swift

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

// MyEditorView.swift

TextEditor(text: $text)
    .attributedTextFormattingDefinition(MyTextFormattingDefinition())
```

> Note: A ``Binding`` to the text of a view with an
> ``AttributedTextFormattingDefinition`` may still contain values that do
> not adhere to text formatting definition. E.g., a ``TextEditor`` may
> choose to not apply constraints in the text formatting definition to
> parts of a bound attributed string that are not visible on screen.

To manually enforce constraints, e.g. before serializing text contents,
use the `AttributedTextFormattingDefinition/constrain(_:)-(AttributedString)`
method.

---

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)