Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.48 KB | None | 0 0
  1. //
  2. // MinusKeyPadTextField.swift
  3. // DecimalMinusDemo
  4. //
  5. // Created by MinjunJu on 06/08/2019.
  6. // Copyright © 2019 Jonathan Engelsma. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. final class MinusKeypadTextField: UITextField {
  12. private let accessContainerView = UIView().then {
  13. $0.backgroundColor = .blue
  14. $0.frame = UI.accessContainerViweFrame
  15. }
  16.  
  17. private let minusButton = UIButton().then {
  18. $0.setTitle("-", for: .normal)
  19. $0.setTitleColor(.white, for: .normal)
  20. $0.frame = UI.minusButtonFrame
  21.  
  22. }
  23.  
  24. private let doneButton = UIButton().then {
  25. $0.setTitle("확인", for: .normal)
  26. $0.setTitleColor(.white, for: .normal)
  27. $0.frame = UI.doneButtonFrame
  28. }
  29.  
  30. var _accessContainerViewBackgroundColor: UIColor = .gray
  31.  
  32. var accessBackgroundColor: UIColor {
  33. get { return _accessContainerViewBackgroundColor }
  34. set {
  35. _accessContainerViewBackgroundColor = newValue
  36. accessContainerView.backgroundColor = newValue
  37. }
  38. }
  39.  
  40.  
  41. enum UI {
  42. static let accessContainerViweFrame = CGRect(
  43. x: 0,
  44. y: 0,
  45. width: UIScreen.mainWidth,
  46. height: 44.dynamicHeight
  47. )
  48. static let minusButtonFrame = CGRect(
  49. x: 0,
  50. y: 0,
  51. width: (UIScreen.mainWidth / 3).r,
  52. height: 44.dynamicHeight
  53. )
  54. static let doneButtonFrame = CGRect(
  55. x: UIScreen.mainWidth - (UIScreen.mainWidth / 3).r ,
  56. y: 0,
  57. width: (UIScreen.mainWidth / 3).r,
  58. height: 44.dynamicHeight
  59. )
  60. }
  61.  
  62. init(keyboardType innerKeyboardType: UIKeyboardType = .numberPad, frame: CGRect = .zero) {
  63. super.init(frame: frame)
  64.  
  65. // 아직 각 키보드 타입에 따른 대응 미 대응
  66. switch innerKeyboardType {
  67. case .`default`, .asciiCapable, .numbersAndPunctuation, .URL, .numberPad, .phonePad,
  68. .namePhonePad, .emailAddress, .decimalPad, .twitter, .webSearch, .asciiCapableNumberPad:
  69. keyboardType = .numberPad
  70. @unknown default:
  71. print("뭐지..")
  72. }
  73. setupUI()
  74. setupBinding()
  75. }
  76.  
  77. override func draw(_ rect: CGRect) {
  78. super.draw(rect)
  79. }
  80.  
  81. required init?(coder aDecoder: NSCoder) {
  82. fatalError("init(coder:) has not been implemented")
  83. }
  84.  
  85. private func setupUI() {
  86. accessContainerView.backgroundColor = accessBackgroundColor
  87. accessContainerView.addSubviews(minusButton, doneButton)
  88. inputAccessoryView = accessContainerView
  89. }
  90.  
  91. private func setupBinding() {
  92. minusButton.addTarget(self, action: #selector(minusButtonAction(_:)), for: .touchUpInside)
  93. doneButton.addTarget(self, action: #selector(doneButtonAction(_:)), for: .touchUpInside)
  94. }
  95.  
  96. @objc func minusButtonAction(_ sender: UIButton) {
  97. guard let text = text else {
  98. print("올수없음")
  99. return
  100. }
  101.  
  102. switch text.count > 0 {
  103. case true:
  104. let index: String.Index = text.index(text.startIndex, offsetBy: 1)
  105. let firstChar = text[..<index]
  106. switch firstChar == "-" {
  107. case true: self.text = String(text[index...])
  108. case false: self.text = "-" + text
  109. }
  110. case false:
  111. break
  112. }
  113. }
  114.  
  115. @objc func doneButtonAction(_ sender: UIButton) {
  116. resignFirstResponder()
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement