Skip to content

Instantly share code, notes, and snippets.

@devmjun
Created August 6, 2019 11:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devmjun/7d84b759ab5c1c392fc20916a08fb5f1 to your computer and use it in GitHub Desktop.
Save devmjun/7d84b759ab5c1c392fc20916a08fb5f1 to your computer and use it in GitHub Desktop.
number-pad-keyboard
//
// 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()
}
}
@devmjun
Copy link
Author

devmjun commented Aug 6, 2019

Screen Shot 2019-08-06 at 8 35 08 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment