A view that displays one or more lines of read-only text.
- iOS 13.0+
- macOS 10.15+
- Mac Catalyst 13.0+
- tvOS 13.0+
- watchOS 6.0+
- Xcode 11.0+
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
caption, using the
font(_:) view modifier.
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
italic() to further adjust the formatting.
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: 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:
Use modifiers like
truncation 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:
If you initialize a text view with a string literal, the view uses the
init(_: 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.
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
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
Localized instance first, which triggers the
init(_: initializer instead:
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.