Structure

Text

A view that displays one or more lines of read-only text.

Declaration

@frozen struct Text

Overview

A text view draws a string in your app’s user interface using a body font that’s appropriate for the current platform. You can choose a different standard font, like title or caption, using the font(_:) view modifier.

Text("Hamlet")
    .font(.title)
A text view showing the name "Hamlet" in a title font.

If you need finer control over the styling of the text, you can use the same modifier to configure a system font or choose a custom font. You can also apply view modifiers like bold() or italic() to further adjust the formatting.

Text("by William Shakespeare")
    .font(.system(size: 12, weight: .light, design: .serif))
    .italic()
A text view showing by William Shakespeare in a 12 point, light, italic, serif font.

A text view always uses exactly the amount of space it needs to display its rendered contents, but you can affect the view’s layout. For example, you can use the frame(width:height:alignment:) modifier to propose specific dimensions to the view. If the view accepts the proposal but the text doesn’t fit into the available space, the view uses a combination of wrapping, tightening, scaling, and truncation to make it fit. With a width of 100 points but no constraint on the height, a text view might wrap a long string:

Text("To be, or not to be, that is the question:")
    .frame(width: 100)
A text view showing a quote from Hamlet split over three lines.

Use modifiers like lineLimit(_:), allowsTightening(_:), minimumScaleFactor(_:), and truncationMode(_:) to configure how the view handles space constraints. For example, combining a fixed width and a line limit of 1 results in truncation for text that doesn’t fit in that space:

Text("Brevity is the soul of wit.")
    .frame(width: 100)
    .lineLimit(1)
A text view showing a truncated quote from Hamlet starting Brevity is t and ending with three dots.

Localizing Strings

If you initialize a text view with a string literal, the view uses the init(_:tableName:bundle:comment:) initializer, which interprets the string as a localization key and searches for the key in the table you specify, or in the default table if you don’t specify one.

Text("pencil") // Searches the default table in the main bundle.

For an app localized in both English and Spanish, the above view displays “pencil” and “lápiz” for English and Spanish users, respectively. If the view can’t perform localization, it displays the key instead. For example, if the same app lacks Danish localization, the view displays “pencil” for users in that locale. Similarly, an app that lacks any localization information displays “pencil” in any locale.

To explicitly bypass localization for a string literal, use the init(verbatim:) initializer.

Text(verbatim: "pencil") // Displays the string "pencil" in any locale.

If you intialize a text view with a variable value, the view uses the init(_:) initializer, which doesn’t localize the string. However, you can request localization by creating a LocalizedStringKey instance first, which triggers the init(_:tableName:bundle:comment:) initializer instead:

// Don't localize a string variable...
Text(writingImplement)

// ...unless you explicitly convert it to a localized string key.
Text(LocalizedStringKey(writingImplement))

When localizing a string variable, you can use the default table by omitting the optional initialization parameters — as in the above example — just like you might for a string literal.

Topics

Creating a Text View

init(LocalizedStringKey, tableName: String?, bundle: Bundle?, comment: StaticString?)

Creates a text view that displays localized content identified by a key.

init<S>(S)

Creates a text view that displays a stored string without localization.

init(verbatim: String)

Creates a text view that displays a string literal without localization.

Choosing a Font

func font(Font?) -> Text

Sets the default font for text in the view.

func fontWeight(Font.Weight?) -> Text

Sets the font weight of the text.

struct Font

An environment-dependent font.

Styling the View’s Text

func foregroundColor(Color?) -> Text

Sets the color of the text displayed by this view.

func bold() -> Text

Applies a bold font weight to the text.

func italic() -> Text

Applies italics to the text.

func strikethrough(Bool, color: Color?) -> Text

Applies a strikethrough to the text.

func underline(Bool, color: Color?) -> Text

Applies an underline to the text.

func kerning(CGFloat) -> Text

Sets the spacing, or kerning, between characters.

func tracking(CGFloat) -> Text

Sets the tracking for the text.

func baselineOffset(CGFloat) -> Text

Sets the vertical offset for the text relative to its baseline.

Fitting Text into Available Space

func allowsTightening(Bool) -> View

Sets whether text can compress the space between characters when necessary to fit text in a line.

func minimumScaleFactor(CGFloat) -> View

Sets the minimum amount that text scales down to fit the available space.

func truncationMode(TruncationMode) -> View

Sets the truncation mode for lines of text that are too long to fit in the available space.

enum Text.TruncationMode

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

Handling Multiline Text

func lineLimit(Int?) -> View

Sets the maximum number of lines that text can occupy in the view.

func lineSpacing(CGFloat) -> View

Sets the amount of space between lines of text in the view.

enum TextAlignment

An alignment position for text along the horizontal axis.

Controlling the Layout Direction

func flipsForRightToLeftLayoutDirection(Bool) -> View

Sets whether the view flips its contents horizontally when the layout direction is right-to-left.

Combining Text Views

static func + (Text, Text) -> Text

Concatenates the text in two text views in a new text view.

Comparing Text Views

static func == (Text, Text) -> Bool

Indicates whether two text views are equal.

static func != (Text, Text) -> Bool

Indicates whether two text views are not equal.

Applying Standard Modifiers

View Modifiers

Configure this view and the views it contains by applying standard modifiers.

Relationships

Conforms To

See Also

Text

struct TextField

A control that displays an editable text interface.

struct SecureField

A control into which the user securely enters private text.

struct Font

An environment-dependent font.