swift - SwiftUI:如何在 TextEditor 中禁用“智能引号”
问题描述
我正在使用 SwiftUI 为 MacOS 开发基于 Python 的图形计算器。
https://github.com/snakajima/macplot
我正在使用 SwiftUI 的 TextEditor 作为 Python 代码的编辑器,但我无法弄清楚如何禁用智能引号(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)
}
}
解决方案
经过几次试验,我想出了以下解决方法。它依赖于 TextEditor 在 NSTextView 之上实现这一事实,并在整个应用程序中改变其行为。这很丑陋,但有效。
// HACK to work-around the smart quote issue
extension NSTextView {
open override var frame: CGRect {
didSet {
self.isAutomaticQuoteSubstitutionEnabled = false
}
}
}
推荐阅读
- mysql - 如何从mysql获取数据并使用ajax以表格形式显示
- json - 将带有嵌套映射的 JSON 映射到对象
- swift - SwiftUI - 无需点击即可触发菜单(自定义可访问性操作)
- php - 仅获取自定义帖子类型的子条款
- c - 为C中结构内的单个字符分配内存
- r - 列中生成的随机数据
- sql - 根据PostgreSQL中的条件删除数组中的元素
- python - Py2App 错误:ModuleNotFoundError:使用 Pandas 时没有名为“cmath”的模块
- python - tkiner 如何通过单击按钮更改我的窗口的类名
- qt - 用于 Raspberry Pi 4 的交叉编译的 Qt 5.15.2 无法在窗口模式下运行程序