---
title: "attributedTextFormattingDefinition(_:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/attributedtextformattingdefinition(_:)-81jn6"
---

# attributedTextFormattingDefinition(_:)

Apply a text formatting definition to nested views.

## Declaration

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

```

## Discussion

Discussion Applying a text formatting definition to a Text or TextEditor created using the init(_:) or 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 AttributedTextValueConstraints: // 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 constrain(_:) method.
