I am looking for equivalents to control the isEditable and isSelectable properties of NSTextView in SwiftUI's TextEditor.
In AppKit, I can easily make a text view editable or selectable using:
textView.isEditable = isEditable
textView.isSelectable = isSelectable
However, I'm having trouble finding how to achieve similar functionality with a TextEditor in SwiftUI.
Preventing text selection
As of macOS Sonoma 14.3.1,
TextEditordoes not honor thetextSelectionorselectionDisabledmodifiers. We need to resort to other means:allowsHitTesting(false)to prevent the user from selecting the text with the mouse.focusable(false)to prevent the user from focusing theTextEditorby pressing tab.Preventing text editing
To prevent text editing, pass a
Binding.constantto theTextEditor.Demo
Use tab to move focus out of a
TextField. Use ⌃tab (control-tab) to move focus out of aTextEditor.