Thanks to the comments I was able to have something maybe decent:
extension View {
@ViewBuilder
func textSelectable(_ isSelectable: Bool) -> some View {
if isSelectable {
textSelection(.enabled)
} else {
textSelection(.disabled)
}
}
}
You can read the @Asperi answer to figure the protocol
implementation problem. The thing I'm trying to do here is to have this extension so it can be set programmatically in other views, like this:
struct ExampleView: View {
var isTextSelectable = true
var body: some View {
Text(attributedString)
.textSelectable(isTextSelectable) //here
}
}
So then you can call it like this:
ExampleView(isTextSelectable: //true or false, make some calculation maybe)
This follows the advices regarding view identity from Apple derived from the source mentioned in the comments.