18

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

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

4

3 回答 3

38

像这样创建 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
    }
}

在 Interface Builder 中,选择此类作为自定义类来代替 UITextField。

这会导致键盘在字段成为第一响应者时选择表情符号键盘(如果可用)。当然,用户可以随时将键盘改回其他任何东西,但至少它提供了您想要的初始选择。

感谢blld在这里的回答https://stackoverflow.com/a/58537544/1852207

于 2017-06-26T05:51:51.530 回答
4

我已经设法防止用户切换键盘!

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

完整解决方案:

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()
        }
    }
}
于 2020-03-27T23:02:23.367 回答
1

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

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

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

于 2021-10-28T19:51:02.517 回答