Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- override func viewDidLoad() {
- super.viewDidLoad()
- let emojiTextField = UITextField.makeEmojiTextField()
- emojiTextField.delegate = self
- view.addSubview(emojiTextField)
- emojiTextField.anchorCenterSuperview()
- }
- class EmojiTextField: UITextField {
- override var textInputMode: UITextInputMode? {
- for mode in UITextInputMode.activeInputModes {
- if mode.primaryLanguage == "emoji" {
- return mode
- }
- }
- return nil
- }
- }
- extension UITextField {
- static func makeEmojiTextField() -> EmojiTextField {
- let tf = EmojiTextField()
- tf.placeholder = ""
- tf.font = UIFont.systemFont(ofSize: 64)
- tf.borderStyle = UITextField.BorderStyle.none
- tf.autocorrectionType = UITextAutocorrectionType.no
- tf.returnKeyType = UIReturnKeyType.done
- tf.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
- tf.backgroundColor = .green
- return tf
- }
- }
- extension UIView {
- public func anchorCenterXToSuperview(constant: CGFloat = 0) {
- translatesAutoresizingMaskIntoConstraints = false
- if let anchor = superview?.centerXAnchor {
- centerXAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
- }
- }
- public func anchorCenterYToSuperview(constant: CGFloat = 0) {
- translatesAutoresizingMaskIntoConstraints = false
- if let anchor = superview?.centerYAnchor {
- centerYAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
- }
- }
- public func anchorCenterSuperview() {
- anchorCenterXToSuperview()
- anchorCenterYToSuperview()
- }
- }
Add Comment
Please, Sign In to add comment