An attributed string to style and display, in accordance with its attributes.
init(_:)
Availability 有効性
- iOS 15.0+
- iPadOS 15.0+
- macOS 12.0+
- Mac Catalyst 15.0+
- tvOS 15.0+
- watchOS 8.0+
Technology
- Swift
UI
Declaration 宣言
init(_ attributedContent: AttributedString
)
Parameters パラメータ
attributedContent
Discussion 議論
Use this initializer to style text according to attributes found in the specified Attributed
. 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 foreground
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)
}
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)
}
A SwiftUI Text
view renders most of the styles defined by the Foundation attribute inline
, like the strongly
value, which SwiftUI presents as bold text.
Important 重要
Text
uses only a subset of the attributes defined in Attribute
. Text
renders all Inline
attributes except for line
and soft
. It also renders the link
attribute as a clickable link. Text
ignores any other Foundation-defined attributes in an attributed string.
SwiftUI also defines additional attributes in the attribute scope Attribute
which you can access from an attributed string’s swift
property. SwiftUI attributes take precedence over equivalent attributes from other frameworks, such as Attribute
and Attribute
.
You can create an Attributed
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 inline
attribute with the value strongly
. 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 Open
in the text view’s environment.
You can also use Markdown syntax in localized string keys, which means you can write the above example without needing to explicitly create an Attributed
:
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:
syntax.
For details about Markdown syntax support in SwiftUI, see init(_:
.