<!--
{
  "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/AttributedTextValueConstraint",
  "metadataVersion" : "0.1.0",
  "role" : "Protocol",
  "symbol" : {
    "kind" : "Protocol",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI29AttributedTextValueConstraintP"
  },
  "title" : "AttributedTextValueConstraint"
}
-->

# AttributedTextValueConstraint

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

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

## Overview

Used as an [`AttributedTextFormattingDefinition`](/documentation/SwiftUI/AttributedTextFormattingDefinition), this constrains the
[`AttributeKey`](/documentation/SwiftUI/AttributedTextValueConstraint/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`](/documentation/SwiftUI/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
[`AttributedTextFormattingDefinition`](/documentation/SwiftUI/AttributedTextFormattingDefinition)s.

```
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`](/documentation/SwiftUI/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`](/documentation/SwiftUI/AttributedTextFormattingDefinition/body-1b01t).

```
/// 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

[`associatedtype AttributeKey : AttributedStringKey`](/documentation/SwiftUI/AttributedTextValueConstraint/AttributeKey)

The attribute constrained by this constraint.

### Instance Methods

[`func constrain(inout Self.Attributes)`](/documentation/SwiftUI/AttributedTextValueConstraint/constrain(_:))

Enforce constraints on the attribute.

### Type Aliases

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

A proxy type for a container of partially constrained attributes.

## Relationships

### Inherits From

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

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

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

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

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

### Conforming Types

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

---

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)