1

maybe a absolute beginner question: In the following code is foregroundColor working but .textSelection doesn't. What is the reason?

Text("This is a Test")
  .foregroundColor(isSelectable ? .green : .red)
  .textSelection(isSelectable ? .enabled : .disabled)

Asperi
  • 228,894
  • 20
  • 464
  • 690
Michael
  • 616
  • 5
  • 20
  • Does this answer your question? [Conditionally searchable Table in SwiftUI?](https://stackoverflow.com/questions/75001678/conditionally-searchable-table-in-swiftui) – lorem ipsum May 22 '23 at 21:20

2 Answers2

2

We cannot put it ternary operator, because .enabled and .disabled are of different concrete types (confirming to one protocol), so possible variant is

let text = "This is a Test"
Group {
    if isSelectable {
        Text(text)
          .textSelection(.enabled)
    } else {
        Text(text)
          .textSelection(.disabled)
    }
}
.foregroundColor(isSelectable ? .green : .red)

Note: actually Apple does not consider this feature as togglable, let's read the doc

/// A selectability value that enables text selection by a person using your app.
///
/// Enabling text selection allows people to perform actions on the text
/// content, such as copying and sharing. Enable text selection in views
/// where those operations are useful, such as copying unique IDs or
/// error messages. This allows people to paste the data into
/// emails or documents.

It's hardly imaginable that "useful informations electability" can be turned of for some reason. Just in case.

Asperi
  • 228,894
  • 20
  • 464
  • 690
  • I don't understand this, I'm tryin to have something as `var textSelectability: TextSelectability = .enabled` but that doesn't work. And it doesn't make sense for me because the source code says `public func textSelection(_ selectability: S) -> some View where S : TextSelectability` so as long as the argument is an implementation from `TextSelectability` it should be the correct type. And `enabled` and `disabled` both are implementations of the protocol. – cutiko May 22 '23 at 18:51
1

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.

cutiko
  • 9,887
  • 3
  • 45
  • 59