AttributedStringKey
A type that defines an attribute’s name and type.
Declaration
protocol AttributedStringKey : SendableMetatypeOverview
You don’t instantiate types that conform to this protocol. Rather, dynamic member lookup uses this type as the basis for looking up key paths on AttributedString subtypes when using an AttributeScope type parameter. When it also conforms to CodableAttributedStringKey, (or DecodableAttributedStringKey/EncodableAttributedStringKey if the type isn’t fully codable), the AttributedStringKey describes which attributes of an AttributedString support encoding or decoding.
Attribute owners — typically frameworks — declare a key like the following:
enum OutlineColorAttribute : AttributedStringKey {
typealias Value = Color
static let name = "OutlineColor"
}Callers can use these types to get attribute values from attributed strings, but typically you want to reference them by name. Attribute owners enable this by creating one or more structures that conform to AttributeScope, in which they provide short names for their attributes that map to the AttributedStringKey type. The following example shows how to do this:
struct MyTextStyleAttributes : AttributeScope {
let outlineColor : OutlineColorAttribute // OutlineColorAttribute.Value == Color
let shadowColor : ShadowColorAttribute // ShadowColorAttribute.Value == Color
// etc.
}After you extend AttributeScope like this, extend AttributeDynamicLookup to allow callers to use dynamic member lookup syntax, like myAttributedString.outlineColor = .red.