Contents

attributed

An attributed format style based on the integer format style.

Declaration

var attributed: IntegerFormatStyle<Value>.Attributed { get }

Discussion

Use this modifier to create an IntegerFormatStyle.Attributed instance, which formats values as AttributedString instances. These attributed strings contain attributes from the AttributeScopes.FoundationAttributes.NumberFormatAttributes attribute scope. Use these attributes to determine which runs of the attributed string represent different parts of the formatted value.

The following example finds runs of the attributed string that represent different parts of a formatted currency, and adds additional attributes like foregroundColor and inlinePresentationIntent.

func attributedPrice(price: Int) -> AttributedString {
    var attributedPrice = price.formatted(
        .currency(code: "USD")
        .attributed)

    for run in attributedPrice.runs {
        if run.attributes.numberSymbol == .currency ||
            run.attributes.numberSymbol == .decimalSeparator {
            attributedPrice[run.range].foregroundColor = .red
        }
        if run.attributes.numberPart == .integer ||
            run.attributes.numberPart == .fraction {
            attributedPrice[run.range].inlinePresentationIntent = [.stronglyEmphasized]
        }
    }
    return attributedPrice
}

User interface frameworks like SwiftUI can use these attributes when presenting the attributed string, as seen here:

[Image]

See Also

Creating attributed strings