TextField set behavior request

Hello there!

I come from Objective-C and there I could have an NSTextField set to not be editable or selectable:

NSTextField *outputfield;
    
[outputfield setEditable:false];
[outputfield setSelectable:false];

You can do the same in Xcode's Interface Builder panel and select options from a Picker to (Selectable, Editable or None):

There is no way to achieve this in SwiftUI and the only option you have is to set a TextField() as .disabled which is not the same. I often used this setters to have an evenly looking form but where only output text (that's not meant to be edited) is show but is clearly visible. The text inside a .disabled text field has no contrast and is not clearly visible.

Therefore I would like to request an addition of these two modifiers:

selectable(_ selectable: Bool)

editable(_ editable: Bool)

I am looking forward to fully switch to SwiftUI therefore whenever I encounter something that I could do in Obj-c/Cocoa but not in SwiftUI I will evaluate and suggest it as an addition. Thanks!

Answered by Frameworks Engineer in 792083022

Hi! You can use the allowsHitTesting(_:) view modifier to disable user interaction with any view, including text fields:

TextField(...)
    .allowsHitTesting(false)

Let me know if this is what you were looking for!

Accepted Answer

Hi! You can use the allowsHitTesting(_:) view modifier to disable user interaction with any view, including text fields:

TextField(...)
    .allowsHitTesting(false)

Let me know if this is what you were looking for!

Thank you! This achieves what I was looking for. This modifier's name is not that intuitive though. 😋

TextField set behavior request
 
 
Q