我们有一个使用UITextInput,UIKeyInput和UITextInputTraits协议实现的文本输入视图。
基本的文本输入和删除工作正常,通过insertText:和deleteBackward。但是,这些方法不会收到 QuickType 建议,而且我无法找到有关 QuickType 如何与这些协议交互的文档。
我应该如何实现这些协议来接收 QuickType 输入?
我正在尝试在 Swift 3 中采用UITextInputTraitsfor 自定义UIButton子类。
我添加了文档“符号”部分中指定的所有属性:
import UIKit
class TextInputButton: UIButton {
var autocapitalizationType: UITextAutocapitalizationType = .none
var autocorrectionType: UITextAutocorrectionType = .no
var spellCheckingType: UITextSpellCheckingType = .no
var enablesReturnKeyAutomatically: Bool = false
var keyboardAppearance: UIKeyboardAppearance = .default
var keyboardType: UIKeyboardType = .default
var returnKeyType: UIReturnKeyType = .default
var isSecureTextEntry: Bool = false
// (...rest of class implementation omitted...)
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试编译时,isSecureTextEntry会导致以下错误:
由 setter 为“isSecureTextEntry”提供的 Objective-C 方法“setIsSecureTextEntry:”与需求的选择器(“setSecureTextEntry:”)不匹配
如果我使用建议的“修复”,则属性声明将变为:
var @objc(setSecureTextEntry:) isSecureTextEntry: Bool = false
Run Code Online (Sandbox Code Playgroud)
...但现在我得到了三个错误: …
我有一个基于自定义UITextInput的文本编辑器。除了通过标记文本进行多阶段输入之外,它工作得很好。
我的标记区域渲染正确,并且插入了标记文本,但键盘上方的候选列表是空白的。
例如,下面是日语(假名)键盘,显示了有关标准 UITextView 的建议:

这是我的自定义编辑器,显示相同的标记文本:

我花了几天时间调试这个问题,发现原因是该方法UIKeyboardImpl返回私有类NOdelegateSupportsCorrectionUI
如果我在类别中覆盖此方法UIKeyboardImpl并返回YES,则多级输入建议会正确显示在我的文本编辑器中。然而,这并没有解决问题的根本原因(而且它不可用)。
我还仔细研究了 Apple 的 SimpleTextInput 示例代码。这实现了一个基本的核心文本编辑器。SimpleTextInput 正确显示多级输入建议,但是我似乎找不到其实现中的任何差异,UITextInput导致它工作而我的崩溃。
(事实上,我无法“破坏”SimpleTextInput 示例显示多阶段输入的能力。这让我认为我对实现的关注UITextInput是错误的。而且这完全是另一回事。)
我正在使用 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)
}
}
Run Code Online (Sandbox Code Playgroud) ios ×3
swift ×2
uitextinput ×2
appkit ×1
cocoa-touch ×1
objective-c ×1
quicktype ×1
swiftui ×1
uikeyboard ×1
uikeyinput ×1