Skip to content

Instance Property

attributed

iOS 15.0+Deprecated iPadOS 15.0+Deprecated Mac Catalyst 15.0+Deprecated macOS 12.0+Deprecated tvOS 15.0+Deprecated watchOS 8.0+Deprecated

Deprecated

Use attributedStyle instead

A type-erased attributed variant of this style.
var attributed: Date.AttributedStyle { get }

Discussion

Use a Date.FormatStyle instance to customize the lexical representation of a date as a string. Use the format style’s attributed property to customize the visual representation of the date as a string. Attributed strings can represent the subcomponent characters, words, and phrases of a string with a custom combination of font size, weight, and color.

For example, the function below uses a date format style to create a custom lexical representation of a date, then retrieves an attributed string representation of the same date and applies a visual emphasis to the year component of the date.

private func makeAttributedString() -> AttributedString {
    let date = Date()
    let formatStyle = Date.FormatStyle(date: .abbreviated, time: .standard)
    var attributedString = formatStyle.attributed.format(date)
    for run in attributedString.runs {
        if let dateFieldAttribute = run.attributes.foundation.dateField,
           dateFieldAttribute == .year {
            attributedString[run.range].inlinePresentationIntent = [.emphasized, .stronglyEmphasized]
        }
    }
    return attributedString
}