Contents

appendInterpolation(_:)

Appends an attributed string to a string interpolation.

Declaration

mutating func appendInterpolation(_ attributedString: AttributedString)

Parameters

  • attributedString:

    The attributed string to append.

Discussion

Don’t call this method directly; it’s used by the compiler when interpreting string interpolations.

The following example shows how to use a string interpolation to format an AttributedString and append it to static text. The resulting interpolation implicitly creates a LocalizedStringKey, which a Text view uses to provide its content.

struct ContentView: View {

    var nextDate: AttributedString {
        var result = Calendar.current
            .nextWeekend(startingAfter: Date.now)!
            .start
            .formatted(
                .dateTime
                .month(.wide)
                .day()
                .attributed
            )
        result.backgroundColor = .green
        result.foregroundColor = .white
        return result
    }

    var body: some View {
        Text("Our next catch-up is on \(nextDate)!")
    }
}

For this example, assume that the app runs on a device set to a Russian locale, and has the following entry in a Russian-localized Localizable.strings file:

"Our next catch-up is on %@!" = "Наша следующая встреча состоится %@!";

The attributed string nextDate replaces the format specifier %@, maintaining its color and date-formatting attributes, when the Text view renders its contents:

[Image]

See Also

Appending to an interpolation