Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // MinusKeyPadTextField.swift
- // DecimalMinusDemo
- //
- // Created by MinjunJu on 06/08/2019.
- // Copyright © 2019 Jonathan Engelsma. All rights reserved.
- //
- import UIKit
- final class MinusKeypadTextField: UITextField {
- private let accessContainerView = UIView().then {
- $0.backgroundColor = .blue
- $0.frame = UI.accessContainerViweFrame
- }
- private let minusButton = UIButton().then {
- $0.setTitle("-", for: .normal)
- $0.setTitleColor(.white, for: .normal)
- $0.frame = UI.minusButtonFrame
- }
- private let doneButton = UIButton().then {
- $0.setTitle("확인", for: .normal)
- $0.setTitleColor(.white, for: .normal)
- $0.frame = UI.doneButtonFrame
- }
- var _accessContainerViewBackgroundColor: UIColor = .gray
- var accessBackgroundColor: UIColor {
- get { return _accessContainerViewBackgroundColor }
- set {
- _accessContainerViewBackgroundColor = newValue
- accessContainerView.backgroundColor = newValue
- }
- }
- enum UI {
- static let accessContainerViweFrame = CGRect(
- x: 0,
- y: 0,
- width: UIScreen.mainWidth,
- height: 44.dynamicHeight
- )
- static let minusButtonFrame = CGRect(
- x: 0,
- y: 0,
- width: (UIScreen.mainWidth / 3).r,
- height: 44.dynamicHeight
- )
- static let doneButtonFrame = CGRect(
- x: UIScreen.mainWidth - (UIScreen.mainWidth / 3).r ,
- y: 0,
- width: (UIScreen.mainWidth / 3).r,
- height: 44.dynamicHeight
- )
- }
- init(keyboardType innerKeyboardType: UIKeyboardType = .numberPad, frame: CGRect = .zero) {
- super.init(frame: frame)
- // 아직 각 키보드 타입에 따른 대응 미 대응
- switch innerKeyboardType {
- case .`default`, .asciiCapable, .numbersAndPunctuation, .URL, .numberPad, .phonePad,
- .namePhonePad, .emailAddress, .decimalPad, .twitter, .webSearch, .asciiCapableNumberPad:
- keyboardType = .numberPad
- @unknown default:
- print("뭐지..")
- }
- setupUI()
- setupBinding()
- }
- override func draw(_ rect: CGRect) {
- super.draw(rect)
- }
- required init?(coder aDecoder: NSCoder) {
- fatalError("init(coder:) has not been implemented")
- }
- private func setupUI() {
- accessContainerView.backgroundColor = accessBackgroundColor
- accessContainerView.addSubviews(minusButton, doneButton)
- inputAccessoryView = accessContainerView
- }
- private func setupBinding() {
- minusButton.addTarget(self, action: #selector(minusButtonAction(_:)), for: .touchUpInside)
- doneButton.addTarget(self, action: #selector(doneButtonAction(_:)), for: .touchUpInside)
- }
- @objc func minusButtonAction(_ sender: UIButton) {
- guard let text = text else {
- print("올수없음")
- return
- }
- switch text.count > 0 {
- case true:
- let index: String.Index = text.index(text.startIndex, offsetBy: 1)
- let firstChar = text[..<index]
- switch firstChar == "-" {
- case true: self.text = String(text[index...])
- case false: self.text = "-" + text
- }
- case false:
- break
- }
- }
- @objc func doneButtonAction(_ sender: UIButton) {
- resignFirstResponder()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement