How to select Text in SwiftUI on macOS

I have (inside a VStack) an HStack, which contains a few Texts.
Displays nicely.
But: I cannot select this line of text (and by implication: cannot copy the line).

Before SwiftUI I would have used an NSTextField with isSelectable = true.

How can this be done in SwiftUI ?

Gerriet.

3 Likes

Currently, SwiftUI doesn't seem to have an option to select text from the Text View and you may have to resort to the old ways.

One way to allow copying strings from the Text View using SwiftUI is as below:

Text(stringDisplayed)
.contextMenu {
Button(action: {
UIPasteboard.general.string = stringDisplayed
}) {
Text("Copy")
}
}

1 Like