Black Friday 2024 deal: 30% off our Swift and SwiftUI books! Learn more ...Black Friday 2024 deal:30% off our Swift and SwiftUI books >>
Quick Tip Icon
Quick Tip

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.

On macOS users can select a range of text using the mouse. On iOS it's only possible to select the entire contents of a Text view by long pressing on it and summoning the system menu with context appropriate actions such as Copy and Share.

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Black Friday 2024 offer: 30% off!$35$25

100+ tips to take your Swift code to the next level

Swift Gemsby Natalia Panferova

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms
Black Friday 2024 offer: 30% off!

100+ tips to take your Swift code to the next level

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Swift Gems

by Natalia Panferova

$35$25