Declaration
compose().textSelection(
selectability: SwiftUI.TextSelectability
)nonisolated func textSelection<S>(
_ selectability: S
) -> some View
where S : TextSelectabilityParameters
selectability*SwiftUI.TextSelectability
Discussion
People sometimes need to copy useful information from Text views ---
including error messages, serial numbers, or IP addresses --- so they
can then paste the text into another context. Enable text selection
to let people select text in a platform-appropriate way.
You can apply this method to an individual text view, or to a container to make each contained text view selectable. In the following example, the person using the app can select text that shows the date of an event or the name or email of any of the event participants:
var body: some View {
VStack {
Text("Event Invite")
.font(.title)
Text(invite.date.formatted(date: .long, time: .shortened))
.textSelection(.enabled)
List(invite.recipients) { recipient in
VStack (alignment: .leading) {
Text(recipient.name)
Text(recipient.email)
.foregroundStyle(.secondary)
}
}
.textSelection(.enabled)
}
.navigationTitle("New Invitation")
}On macOS, people use the mouse or trackpad to select a range of text, which they can quickly copy by choosing Edit > Copy, or with the standard keyboard shortcut.
On iOS, the person using the app touches and holds on a selectable
Text view to begin selecting text.
In iOS 27 or later, this brings up the system text selection handles
and highlight which supports selecting a range of text.
In iOS 26 or earlier, this brings up a system menu with
menu items appropriate for the current context.
These menu items operate on the entire contents of the Text view.
@Image(source: "View-textSelection-2", alt: "A portion of an iOS view, with header Event Invite and the date and time of the event below it. Below the date and time, a menu shows three items: Copy, Look Up and Translate. Below this, a list of invitees by name and email.", deviceFrame: "iPhone16-top-half")
See also
More in Text and symbols.
- allowsTighteningSets whether text in this view can compress the space between characters when necessary to fit text in a line.
- autocorrectionDisabledSets whether to disable autocorrection for this view.
- baselineOffsetSets the vertical offset for the text relative to its baseline in this view.
- boldApplies a bold font weight to the text in this view.
- findDisabledPrevents find and replace operations in a text editor.
- findNavigatorProgrammatically presents the find and replace interface for text editor views.
- fontSets the default font for text in this view.
- fontDesignSets the font design of the text in this view.
- fontWeightSets the font weight of the text in this view.
- fontWidthSets the font width of the text in this view.
- interactionActivityTrackingTagSets a tag that you use for tracking interactivity.
- italicApplies italics to the text in this view.