UITextField for Emoji Input & Unchangeable Keyboard Type in Swift IOS

In this article, we will learn UITextField for Emoji Input & Unchangeable Keyboard Type in Swift IOS. In most applications, we use the UITextfield to get user values such as name, email address, or other details. Users type emoji so it is not valid and we have a lot of restrictions on this type of string.

Following custom, UITextfield can be used to only show Emojis on the keyboard with unchangeable keyboard type.

Emoji Input & Unchangeable Keyboard Type in Swift IOS - Handy Opinion

Code

textInputContextIdentifier is an identifier that indicates that the respondent should keep information about the text entry method. If the user changes the language then again the emoji keyboard will appear because this is an unchangeable keyboard.
addObserver add items to the notification center to receive notifications forwarded to a specific block.

import UIKit

class EmojiTextField: UITextField {

       // required for iOS 13
       override var textInputContextIdentifier: String? { "" }

        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: NSNotification.Name.UITextInputCurrentInputModeDidChange, object: nil)
        }

        @objc func inputModeDidChange(_ notification: Notification) {
            guard isFirstResponder else {
                return
            }

            DispatchQueue.main.async { [weak self] in
                self?.reloadInputViews()
            }
        }
    }

How to Use

Create a new class in your swift project with the name EmojiTextField and paste the above code inside it. then add a normal UITextField in your Storyboard and add EmojiTextField as its class in inspector view, as shown in the following screenshot.

That’s it. This is how used UITextField for Emoji Input & Unchangeable Keyboard Type in Swift IOS

If you have any questions or suggestions, feel free to ask in the comments section below.

Next Article

1. Get Device Identifier and save in Keychain in Swift IOS
2. How to Use Diffable Data Source with UI TableView in Swift IOS

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: