Protocol
AttributedStringKey
protocol AttributedStringKey : SendableMetatype
Overview
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.
Topics
Associated Types
associatedtype Value : HashableThe type of the key’s value.
Instance Properties
Type Properties
Relationships
Inherits From
Swift.SendableMetatype
Inherited By
Conforming Types
AttributeScopes.FoundationAttributes.AlternateDescriptionAttributeAttributeScopes.FoundationAttributes.ByteCountAttributeAttributeScopes.FoundationAttributes.DateFieldAttributeAttributeScopes.FoundationAttributes.DurationFieldAttributeAttributeScopes.FoundationAttributes.ImageURLAttributeAttributeScopes.FoundationAttributes.LanguageIdentifierAttributeAttributeScopes.FoundationAttributes.LinkAttributeAttributeScopes.FoundationAttributes.MeasurementAttributeAttributeScopes.FoundationAttributes.NumberFormatAttributes.NumberPartAttributeAttributeScopes.FoundationAttributes.NumberFormatAttributes.SymbolAttributeAttributeScopes.FoundationAttributes.PersonNameComponentAttributeAttributeScopes.FoundationAttributes.ReplacementIndexAttributeAttributeScopes.FoundationAttributes.WritingDirectionAttribute