Guest User

Untitled

a guest
Dec 14th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. override func viewDidLoad() {
  2. super.viewDidLoad()
  3. let emojiTextField = UITextField.makeEmojiTextField()
  4. emojiTextField.delegate = self
  5.  
  6. view.addSubview(emojiTextField)
  7. emojiTextField.anchorCenterSuperview()
  8. }
  9.  
  10. class EmojiTextField: UITextField {
  11. override var textInputMode: UITextInputMode? {
  12. for mode in UITextInputMode.activeInputModes {
  13. if mode.primaryLanguage == "emoji" {
  14. return mode
  15. }
  16. }
  17. return nil
  18. }
  19. }
  20.  
  21. extension UITextField {
  22. static func makeEmojiTextField() -> EmojiTextField {
  23. let tf = EmojiTextField()
  24. tf.placeholder = ""
  25. tf.font = UIFont.systemFont(ofSize: 64)
  26. tf.borderStyle = UITextField.BorderStyle.none
  27. tf.autocorrectionType = UITextAutocorrectionType.no
  28. tf.returnKeyType = UIReturnKeyType.done
  29. tf.contentVerticalAlignment = UIControl.ContentVerticalAlignment.center
  30. tf.backgroundColor = .green
  31. return tf
  32. }
  33. }
  34.  
  35. extension UIView {
  36. public func anchorCenterXToSuperview(constant: CGFloat = 0) {
  37. translatesAutoresizingMaskIntoConstraints = false
  38. if let anchor = superview?.centerXAnchor {
  39. centerXAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
  40. }
  41. }
  42.  
  43. public func anchorCenterYToSuperview(constant: CGFloat = 0) {
  44. translatesAutoresizingMaskIntoConstraints = false
  45. if let anchor = superview?.centerYAnchor {
  46. centerYAnchor.constraint(equalTo: anchor, constant: constant).isActive = true
  47. }
  48. }
  49.  
  50. public func anchorCenterSuperview() {
  51. anchorCenterXToSuperview()
  52. anchorCenterYToSuperview()
  53. }
  54. }
Add Comment
Please, Sign In to add comment