将键盘布局更改为表情符号

ahm*_*mad 6 uikit uikeyboard ios emoji uikeyboardtype

当UITextField成为第一个响应者时,是否可以将键盘布局更改为表情符号?或者根据用户操作,例如点击UIButton

我知道我可以将键盘布局更改为其中一个

typedef enum {
UIKeyboardTypeDefault,                // Default type for the current input method.
UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters, non-ASCII keyboards remain active
UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
UIKeyboardTypeNumberPad,              // A number pad (0-9). Suitable for PIN entry.
UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).

UIKeyboardTypeAlphabet = UIKeyboardTypeASCIICapable, // Deprecated

} UIKeyboardType;
Run Code Online (Sandbox Code Playgroud)

我想知道是否有办法对表情符号布局做同样的事情

Dal*_*ale 19

像这样创建一个UITextField的子类:

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
    }
}
Run Code Online (Sandbox Code Playgroud)

在IB中,选择此类作为自定义类来代替UITextField.

这使得键盘在字段成为第一响应者时选择表情符号键盘(如果可用).当然,用户可以随时将键盘更改回任何其他内容,但至少它会初步选择您想要的内容.


pka*_*amb 7

以下简化适用于 2021 年的 iOS 15:

class EmojiTextField: UITextField {
    override var textInputMode: UITextInputMode? {
        .activeInputModes.first(where: { $0.primaryLanguage == "emoji" })
    }
}
Run Code Online (Sandbox Code Playgroud)

iOS 15 似乎不需要textInputContextIdentifier,因为 iOS 13 在其他答案中被指出需要。表情符号键盘会在未声明的情况下打开。


tal*_*nts 5

我已经找到了一种方法来阻止用户切换键盘!

\n

使用此线程iOS:如何检测键盘更改事件作​​为成分。

\n

完整解决方案:

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

  • 不幸的是,这也会禁用表情符号搜索。 (2认同)