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

当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; 

我想知道是否有办法做到与表情符号布局相同

不,键盘布局是用户的select,用户必须自己激活一个。

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

 class EmojiTextField: UITextField { override var textInputMode: UITextInputMode? { for mode in UITextInputMode.activeInputModes { if mode.primaryLanguage == "emoji" { return mode } } return nil } } 

在IB中select这个类作为自定义类代替UITextField。

这会使键盘在字段变为第一响应者时select表情符号键盘(如果可用)。 用户当然可以随时将键盘改回其他任何东西,但是至less它可以初步select你想要的东西。