所以我正在快速开发一个 tvos 应用程序,我想知道是否可以禁用对自定义 UITextField 的听写支持。它并不能很好地工作,我不希望用户能够这样做
问问题
2004 次
2 回答
0
您是否尝试使用文本字段的键盘类型属性?也许您可以更改文本输入类型,因此听写功能自动不显示。
于 2016-05-27T14:48:52.507 回答
0
这是基于@BadPirate's hack的 Swift 4 解决方案。它将触发初始铃声,表明听写开始,但听写布局永远不会出现在键盘上。
这不会隐藏键盘上的听写按钮:因为唯一的选择似乎是使用带有UIKeyboardType.emailAddress的电子邮件布局。
在viewDidLoad
拥有UITextField
要禁用听写的视图控制器中:
// Track if the keyboard mode changed to discard dictation
NotificationCenter.default.addObserver(self,
selector: #selector(keyboardModeChanged),
name: UITextInputMode.currentInputModeDidChangeNotification,
object: nil)
然后自定义回调:
@objc func keyboardModeChanged(notification: Notification) {
// Could use `Selector("identifier")` instead for idSelector but
// it would trigger a warning advising to use #selector instead
let idSelector = #selector(getter: UILayoutGuide.identifier)
// Check if the text input mode is dictation
guard
let textField = yourTextField as? UITextField
let mode = textField.textInputMode,
mode.responds(to: idSelector),
let id = mode.perform(idSelector)?.takeUnretainedValue() as? String,
id.contains("dictation") else {
return
}
// If the keyboard is in dictation mode, hide
// then show the keyboard without animations
// to display the initial generic keyboard
UIView.setAnimationsEnabled(false)
textField.resignFirstResponder()
textField.becomeFirstResponder()
UIView.setAnimationsEnabled(true)
// Do additional update here to inform your
// user that dictation is disabled
}
于 2018-11-20T18:46:33.870 回答