---
title: AttributedTextValueConstraint
framework: swiftui
role: symbol
role_heading: Protocol
path: swiftui/attributedtextvalueconstraint
---

# AttributedTextValueConstraint

A protocol for defining a constraint on the value of a certain attribute.

## Declaration

```swift
protocol AttributedTextValueConstraint : Hashable, Sendable, AttributedTextFormattingDefinition
```

## Overview

Overview Used as an AttributedTextFormattingDefinition, this constrains the AttributeKey’s value using the constrain(_:)-(Attributes) function. Given value constraints can read other attribute values, it is crucial to avoid mixing value constraints in a way where they create cyclic dependencies with undefined behavior. Thus, it is recommended to think about value constraints in the context of the AttributedTextFormattingDefinition they will be used in: A simple constraint only accesses a single attribute. It can be made generic over the attribute scope so it can be reused in different AttributedTextFormattingDefinitions. struct NoBlackOrWhiteForeground<Scope: AttributeScope>: AttributedTextValueConstraint {     typealias AttributeKey = AttributeScopes.SwiftUIAttributes.ForegroundColorAttribute

func constrain(         _ container: inout Attributes     ) {         if container.foregroundColor == .white || container.foregroundColor == .black {             container.foregroundColor = .primary         }     } } When the constraint needs to access other attribute values, it is recommended to define it on a specific attribute scope that is used for a single AttributedTextFormattingDefinition. extension MyTextFormattingDefinition {     struct Scope: AttributeScope {         /* ... */         let foregroundColor: AttributeScopes.SwiftUIAttributes.ForegroundColorAttribute         let backgroundColor: AttributeScopes.SwiftUIAttributes.BackgroundColorAttribute     } }

struct NoEqualForegroundAndBackground: AttributedTextValueConstraint {     typealias Scope = MyTextFormattingDefinition.Scope     typealias AttributeKey = AttributeScopes.SwiftUIAttributes.BackgroundColorAttribute

func constrain(         _ container: inout Attributes     ) {         if let color = container.foregroundColor,            container.backgroundColor == color         {             container.backgroundColor = nil         }     } } Constraints that access multiple attributes and are generic over the scope should document their dependencies so that the dependencies can be considered for the ordering of constraints in the body. /// Makes the background color for all Genmoji blue. /// /// - Note: This constraint depends on a valid adaptiveImageGlyph value. struct BlueGenmojiBackgroundConstraint<Scope: AttributeScope>: AttributedTextValueConstraint {     typealias AttributeKey = AttributeScopes.SwiftUIAttributes         .BackgroundColorAttribute

func constrain(         _ container: inout Attributes     ) {         if container[             AttributeScopes.SwiftUIAttributes.AdaptiveImageGlyphAttribute.self         ] != nil {             container.backgroundColor = .blue         }     } }

## Topics

### Associated Types

- [AttributeKey](swiftui/attributedtextvalueconstraint/attributekey.md)

### Instance Methods

- [constrain(_:)](swiftui/attributedtextvalueconstraint/constrain(_:).md)

### Type Aliases

- [AttributedTextValueConstraint.Attributes](swiftui/attributedtextvalueconstraint/attributes.md)

## Relationships

### Inherits From

- [AttributedTextFormattingDefinition](swiftui/attributedtextformattingdefinition.md)
- [Equatable](swift/equatable.md)
- [Hashable](swift/hashable.md)
- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)

### Conforming Types

- [AttributedTextFormatting.ValueConstraint](swiftui/attributedtextformatting/valueconstraint.md)

## See Also

### Controlling text style

- [bold(_:)](swiftui/view/bold(_:).md)
- [italic(_:)](swiftui/view/italic(_:).md)
- [underline(_:pattern:color:)](swiftui/view/underline(_:pattern:color:).md)
- [strikethrough(_:pattern:color:)](swiftui/view/strikethrough(_:pattern:color:).md)
- [textCase(_:)](swiftui/view/textcase(_:).md)
- [textCase](swiftui/environmentvalues/textcase.md)
- [monospaced(_:)](swiftui/view/monospaced(_:).md)
- [monospacedDigit()](swiftui/view/monospaceddigit().md)
- [AttributedTextFormattingDefinition](swiftui/attributedtextformattingdefinition.md)
- [AttributedTextFormatting](swiftui/attributedtextformatting.md)
