Change the iOS keyboard layout to emoji?

后端 未结 2 2028
无人及你
无人及你 2020-11-30 06:10

Is it possible to change the keyboard layout to emoji when a UITextField becomes the first responder ? or according to a user action like tapping a UIButton

I know

2条回答
  •  再見小時候
    2020-11-30 06:35

    I have managed a way to prevent user from switching keyboards!

    Used this thread iOS: How to detect keyboard change event as an ingredient.

    Full solution:

    class EmojiTextField: UITextField {
    
           // required for iOS 13
           override var textInputContextIdentifier: String? { "" } // return non-nil to show the Emoji keyboard ¯\_(ツ)_/¯ 
    
            override var textInputMode: UITextInputMode? {
                for mode in UITextInputMode.activeInputModes {
                    if mode.primaryLanguage == "emoji" {
                        return mode
                    }
                }
                return nil
            }
    
        override init(frame: CGRect) {
                super.init(frame: frame)
    
                commonInit()
            }
    
            required init?(coder: NSCoder) {
                super.init(coder: coder)
    
                 commonInit()
            }
    
            func commonInit() {
                NotificationCenter.default.addObserver(self,
                                                       selector: #selector(inputModeDidChange),
                                                       name: UITextInputMode.currentInputModeDidChangeNotification,
                                                       object: nil)
            }
    
            @objc func inputModeDidChange(_ notification: Notification) {
                guard isFirstResponder else {
                    return
                }
    
                DispatchQueue.main.async { [weak self] in
                    self?.reloadInputViews()
                }
            }
        }
    

提交回复
热议问题