Swift’s Text views usually show static, unselectable text by default. Therefore quite often users cannot select the text and copy it, but now you can change that using the .textSelection() modifier with the .enable value.
For example, this makes some text the user can select, and some the user cannot:
var body: some View {
VStack(spacing: 50) {
Text("This text can't be selected")
Text("But this can be selected!")
.textSelection(.enabled)
}
}
Once the text got selected, the user can do regular actions like copy, share etc. However, right now at least the whole text area is copied – you don’t get a text selection loupe, so you can’t select just a few words.
Any kind of group of text views can be made selectable by adding .textSelection() to it. For example, we could make both text views in our previous example selectable by moving the modifier up to the stack.
var body: some View {
VStack(spacing: 50) {
Text("This text can't be selected")
Text("But this can be selected!")
}
.textSelection(.enabled)
}
You can even apply textSelection()
to a list, in which case the text rows in the list become selectable, but in this case you have to press and hold on the text to select.
var body: some View {
List(0..<100) { index in
Text("Member \(index)")
}
.textSelection(.enabled)
}