Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How to disable the "smart quotes" in TextEditor

I'm developing a Python-based graphic calculator for MacOS using SwiftUI.

https://github.com/snakajima/macplot

I am using SwiftUI's TextEditor as the editor for Python code, but I am not able to figure out how to disable the smart quote (UITextInputTraits, smartQuotesType: UITextSmartQuotesType).

        VStack {
            TextEditor(text: $pythonScript.script)
            HStack {
                Button(action: {
                    pythonScript.run(clear: settings.shouldClear)
                }, label: {
                    Text("Plot")
                })
                Toggle("Clear", isOn: $settings.shouldClear)
            }
            if let errorMsg = pythonScript.errorMsg {
                Text(errorMsg)
                    .foregroundColor(.pink)
            }
        }
like image 791
Satoshi Nakajima Avatar asked Jan 24 '26 13:01

Satoshi Nakajima


1 Answers

After several trials, I came up with the following work-around. It relies on the fact that TextEditor is implemented on top of NSTextView, and changes its behavior across the entire application. It is ugly, but works.

// HACK to work-around the smart quote issue
extension NSTextView {
    open override var frame: CGRect {
        didSet {
            self.isAutomaticQuoteSubstitutionEnabled = false
        }
    }
}
like image 140
Satoshi Nakajima Avatar answered Jan 26 '26 02:01

Satoshi Nakajima