How to enable text selection in SwiftUI Text Views to copy it at runtime

Enabling selection of the text inside a Text View of SwiftUI

SOFTWARE DEVELOPMENTIOS DEVELOPMENTSWIFTUI

Eduardo Domínguez Menéndez

7/20/20241 min read

Let's start a new article of my coding school. By default, you may not be able to select the content (text) of a Text View in SwiftUI to trigger the typical context menu with options such as "Copy" or "Translate".

To enable menu with options such as "Translate" or "Copy", the required code snippet is quite simple: add the following modifier to the the Text View or to a hierarchy of views:

.textSelection(.enabled)

This behaviour is easy to apply but is easy to forget this modifier when you learn SwiftUI.