I want to make an emoji text box where it would be possible to write the only emoji and better so that the emoji keyboard appears. The emoji keyboard is standard. I am writing a program in SwiftUI. Found this code, does it work for me? If so, how do you use it? If not, which one do you need?
Thank!
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()
}
}
}