Enable text selection for non-editable text

Sep 18, 2022 1 min read
Enable text selection for non-editable text

To enable text selection for SwiftUI Text views we can use textSelection() modifier and set it to enabled.

This modifier can be set on each Text view individually or on a hierarchy of views to enable text selection for all Text in the hierarchy.

// Contents of the Text view
// will be selectable
Text("Hello World!")
    .textSelection(.enabled)

// Contents of each Text view
// will be selectable individually
VStack {
    Text("Text 1")
    Text("Text 2")
}
.textSelection(.enabled)

Even when we set .textSelection(.enabled) on a hierarchy of views, each Text view will still be individually selectable. There is no way to select contents of multiple Text views at the same time.

Great! Next, complete checkout for full access to ArturoFM.
Welcome back! You've successfully signed in.
You've successfully subscribed to ArturoFM.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info has been updated.
Your billing was not updated.