Select to highlight individual words (e.g. on long press) with SwiftUI

Is there an easy way to allow users to tap on SwiftUI Text elements to highlight specific words e.g. copy ?

The .textSelection API works to select an entire block of text however it'd be great to confirm if there is an existing way with Text for individual words or if Apple plan on adding this ability at some point in the future.

Answered by DTS Engineer in 821717022

On macOS, SwiftUI Text + .textSelection supports double clicking to select a word.

On iOS, TextEditor and TextField support double tapping to select a word as well. Text doesn't have the feature, and so you might consider using UIViewRepresentable + UITextView, if needed.

Regarding "if Apple plan on adding this ability," Apple folks typically don't comment their future product plan.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

On macOS, SwiftUI Text + .textSelection supports double clicking to select a word.

On iOS, TextEditor and TextField support double tapping to select a word as well. Text doesn't have the feature, and so you might consider using UIViewRepresentable + UITextView, if needed.

Regarding "if Apple plan on adding this ability," Apple folks typically don't comment their future product plan.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Select to highlight individual words (e.g. on long press) with SwiftUI
 
 
Q