AttributedText

@dynamicMemberLookup
public struct AttributedText

AttributedText allows you to apply strongly-typed attributes to strings (much like the AttributedString type introduced in iOS 15). You can then access the attributedString property to get an attributed string with those attributes applied.

For example:

var text = AttributedText(string: "Hello, world")
// Apply a font to the entire range
text.font = .systemFont(ofSize: 20)

// Apply a color to part of the string
let range = text.string.range(of: "world")!
text[range].color = .blue

// Render the attributed text
let label = AttributedLabel(attributedText: text.attributedString)
  • The wrapped string, with no attributes.

    Declaration

    Swift

    public let string: String
  • An NSAttributedString representation of the attributed text.

    Declaration

    Swift

    public var attributedString: NSAttributedString { get }
  • An iterable view into segments of the attributed string, each of which indicates where a run of identical attributes begins or ends.

    Declaration

    Swift

    public var runs: [Run] { get }
  • Create some AttributedText from a plain string.

    Declaration

    Swift

    public init(_ string: String)
  • Create some AttributedText from an attributed string. The attributes are preserved.

    Declaration

    Swift

    public init(_ attributedString: NSAttributedString)
  • Declaration

    Swift

    public func range(of aString: some StringProtocol) -> Range<String.Index>?
  • Dynamic member getter or setter for any attributes defined on TextAttributeContainer. Applies the attribute to the entire range of text, for example:

    var text = AttributedText(string: "Hello, world")
    text.font = .systemFont(ofSize: 20)
    

    Note that only attributes applying to the entire range will be returned. For example, if the text has two different font attributes, then text.font will be nil.

    Declaration

    Swift

    public subscript<Value>(dynamicMember keyPath: WritableKeyPath<TextAttributeContainer, Value>) -> Value { get set }
  • Get or set a TextAttributeContainer for the provided range of text. This allows you to set attributes for specific ranges using strong types:

    var text = AttributedText(string: "Hello, world")
    let range = text.string.range(of: "Hello")!
    text[range].font = .systemFont(ofSize: 20)
    

    Note that the returned TextAttributeContainer will only contain attributes that apply to the entire subscript range. (Setting an attribute will set it across the subscript range regardless of any existing contents).

    Declaration

    Swift

    public subscript<R>(range: R) -> TextAttributeContainer where R : RangeExpression, R.Bound == String.Index { get set }
  • Concatenate two pieces of AttributedText together.

    Declaration

    Swift

    public static func + (lhs: AttributedText, rhs: AttributedText) -> AttributedText
  • Run

    A Run represents a range of identical attributes in the attributed text.

    You can access any properties of TextAttributeContainer on this type using dynamic member lookup.

    See more

    Declaration

    Swift

    @dynamicMemberLookup
    public struct Run