【问题标题】:Is there any way to disable dictation support of a UITextField?有什么方法可以禁用 UITextField 的听写支持?
【发布时间】:2016-05-26 16:35:59
【问题描述】:

所以我正在快速开发一个 tvos 应用程序,我想知道是否可以禁用对自定义 UITextField 的听写支持。它真的不适合它,我不希望用户能够这样做

【问题讨论】:

    标签: xcode swift tvos siri-remote


    【解决方案1】:

    您是否尝试使用文本字段的keyboardType 属性?也许您可以更改文本输入类型,因此听写功能自动不显示。

    文档:https://developer.apple.com/library/tvos/documentation/UIKit/Reference/UITextInputTraits_Protocol/index.html#//apple_ref/occ/intfp/UITextInputTraits/keyboardType

    【讨论】:

      【解决方案2】:

      这是一个基于@BadPirate's hack 的 Swift 4 解决方案。它将触发初始铃声,表明听写开始,但听写布局永远不会出现在键盘上。

      这不会从您的键盘上隐藏听写按钮:因为唯一的选择似乎是使用带有UIKeyboardType.emailAddress 的电子邮件布局。


      在拥有UITextField 的视图控制器的viewDidLoad 中,您要为其禁用听写:

      // 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
      }
      

      【讨论】:

        猜你喜欢
        • 2012-12-07
        • 1970-01-01
        • 1970-01-01
        • 2017-03-09
        • 1970-01-01
        • 2013-03-27
        • 2020-02-14
        • 2012-05-07
        • 2016-10-25
        相关资源
        最近更新 更多