textSelection

Controls whether people can select text within this view.

View ModifieriOS 15.0+macOS 12.0+textSelection(_:) in SwiftUI

Declaration

React
compose().textSelection(
  selectability: SwiftUI.TextSelectability
)
Swift
nonisolated func textSelection<S>(
    _ selectability: S
) -> some View
where S : TextSelectability

Parameters

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.