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
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()
}
}
}
* UPDATED FOR iOS13 *
Create a subclass of UITextField like this:
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
}
}
In IB select this class as the Custom Class in place of UITextField.
This causes the keyboard to select emoji keyboard, if available, when the field becomes first responder. The user can, of course, change the keyboard back to anything else at any time, but at least it gives an initial selection of what you want.
Thanks to blld for his answer here https://stackoverflow.com/a/58537544/1852207.