跳转至
Structure

Text

A view that displays read-only text.

Declaration

@frozen struct Text : Equatable

Overview

Text draws a String in your app and comes equipped with modifiers to customize your text. This view sizes itself to fit the provided content, styling and containing view.

struct ExampleView: View {
    var body: some View {
        Text("🍌🍌")
    }
}
A view displaying the text "🍌🍌".

Text is most commonly initialized with a String, however, it has 9 different initializers.

For example, use init(_:style:) to display a date in a Text view.

struct ExampleView: View {
    var body: some View {
        Text(Date(), style: .date)
    }
}
A text view displaying the date.

Text also accepts 10 unique modifiers to customize your string.

struct ExampleView: View {
    var body: some View {
        Text(Date(), style: .date)
            .underline(true, color: .orange)
            .font(.system(size: 20, weight: .bold, design: .rounded))
    }
}
A text view displaying the date with an orange underline and a bolded, rounded text font.

Text conforms to the View protocol. Therefore, any modifiers that return some View, such as foregroundColor(_:), are compatible with Text.

struct ExampleView: View {
    var body: some View {
        Text(Date(), style: .date)
            .font(.system(size: 20, weight: .bold, design: .rounded))
            .foregroundColor(.orange)
    }
}
A text view displaying the date in an orange, bolded, rounded text font.

Remember, any modifier that returns some View must be used after modifiers that return Text.

If your app is localized, you can display localized text by passing the key to the initializer. For example, if you used the localization key of "banana" and mapped it to "🍌🍌" for your current location, the localized string could be displayed with this line:

struct ExampleView: View {
    var body: some View {
        Text("banana")
    }
}
See init(_:tableName:bundle:comment:) for more information on how to initialize Text with localized strings. This initializer can be used to display localized keys coming from a non-standard bundle or string table.

Images can be displayed in a text view. This enables your app to optionally include them inside a text string, where they will resize based on your view's font. See init(_:) for more on initializing Text with images.

Use the View modifiers lineLimit(_:), allowsTightening(_:), minimumScaleFactor(_:), and truncationMode(_:) to configure how Text handles space constraints.

Availability

iOS 13.0+

macOS 10.15+

tvOS 13.0+

watchOS 6.0+

Topics


Initializer

init(_:) Creates an instance that wraps an Image, suitable for concatenating with other Text

init(_:) Creates an instance that displays a localized range between two dates.

init(_:) Creates an instance that displays a localized time interval.

init(_:) Creates a text view that displays a stored string without localization.

init(_:formatter:) Creates a text view that displays the formatted representation of a value.

init(_:formatter:) Creates a text view that displays the formatted representation of a value.

init(_:style:) Creates an instance that displays localized dates and times using a specific style.

init(_:tableName:bundle:comment:) Creates a text view that displays localized content identified by a key.

init(verbatim:) Creates a text view that displays a string literal without localization.


Instance Method

baselineOffset(_:) Sets the vertical offset for the text relative to its baseline.

bold() Applies a bold font weight to the text.

font(_:) Sets the default font for text in the view.

fontWeight(_:) Sets the font weight of the text.

foregroundColor(_:) Sets the color of the text displayed by this view.

italic() Applies italics to the text.

kerning(_:) Sets the spacing, or kerning, between characters.

strikethrough(_:color:) Applies a strikethrough to the text.

tracking(_:) Sets the tracking for the text.

underline(_:color:) Applies an underline to the text.


Structure

DateStyle A predefined style used to display a Date.


Enumeration

Case A scheme for transforming the capitalization of characters within text.

TruncationMode The type of truncation to apply to a line of text when it's too long to fit in the available space.


Type Alias

Body The type of view representing the body of this view.


Type Method

+(lhs:rhs:) Concatenates two text views into one new text view.

==(a:b:) Returns a Boolean value indicating whether two values are equal.