Contents

init(_:)

Creates a text view that displays styled attributed content.

Declaration

init(_ attributedContent: AttributedString)

Parameters

  • attributedContent:

    An attributed string to style and display, in accordance with its attributes.

Format text by combining attributes and view modifiers

Use this initializer to style text according to attributes found in the specified AttributedString. Attributes in the attributed string take precedence over styles added by view modifiers. For example, the attributed text in the following example appears in blue, despite the use of the foregroundColor(_:) modifier to use red throughout the enclosing VStack:

var content: AttributedString {
    var attributedString = AttributedString("Blue text")
    attributedString.foregroundColor = .blue
    return attributedString
}

var body: some View {
    VStack {
        Text(content)
        Text("Red text")
    }
    .foregroundColor(.red)
}

[Image]

SwiftUI combines text attributes with SwiftUI modifiers whenever possible. For example, the following listing creates text that is both bold and red:

var content: AttributedString {
    var content = AttributedString("Some text")
    content.inlinePresentationIntent = .stronglyEmphasized
    return content
}

var body: some View {
    Text(content).foregroundColor(Color.red)
}

Supported Foundation attributes

A SwiftUI Text view renders most of the styles defined by the Foundation attribute inlinePresentationIntent, like the stronglyEmphasized value, which SwiftUI presents as bold text.

SwiftUI attributes

SwiftUI also defines additional attributes in the attribute scope AttributeScopes.SwiftUIAttributes which you can access from an attributed string’s swiftUI property. SwiftUI attributes take precedence over equivalent attributes from other frameworks, such as AttributeScopes.UIKitAttributes and AttributeScopes.AppKitAttributes.

Markdown support

You can create an AttributedString with Markdown syntax, which allows you to style distinct runs within a Text view:

let content = try! AttributedString(
    markdown: "**Thank You!** Please visit our [website](http://example.com).")

var body: some View {
    Text(content)
}

The ** syntax around “Thank You!” applies an inlinePresentationIntent attribute with the value stronglyEmphasized. SwiftUI renders this as bold text, as described earlier. The link syntax around “website” creates a link attribute, which Text styles to indicate it’s a link; by default, clicking or tapping the link opens the linked URL in the user’s default browser. Alternatively, you can perform custom link handling by putting an OpenURLAction in the text view’s environment.

[Image]

You can also use Markdown syntax in localized string keys, which means you can write the above example without needing to explicitly create an AttributedString:

var body: some View {
    Text("**Thank You!** Please visit our [website](https://example.com).")
}

In your app’s strings files, use Markdown syntax to apply styling to the app’s localized strings. You also use this approach when you want to perform automatic grammar agreement on localized strings, with the ^[text](inflect:true) syntax.

For details about Markdown syntax support in SwiftUI, see init(_:tableName:bundle:comment:).

Applying a custom text formatting definition

Use the attributedTextFormattingDefinition(_:) modifier to apply a custom AttributedTextFormattingDefinition to text created using this initializer. This will result in the text only applying attributes in the definition’s attribute scope and constraining attributes according to the definition’s value constraints prior to display.

Custom attributes listed in the definition’s Scope, where the Value conforms to the TextAttribute protocol, can be read when observing the text’s layout using Text/Layout/Run/subscript(key:)->T?, just as text attributes applied using the customAttribute(_:) modifier.

See Also

Creating a text view