Skip to content

Commit fb3d73f

Browse files
vinothkeepworkspavankotesh
authored andcommitted
Support custom number of digits feature
1 parent 1d82d15 commit fb3d73f

9 files changed

Lines changed: 127 additions & 132 deletions

File tree

Example/KWVerificationCodeView/AppDelegate.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
3232
func applicationWillTerminate(_ application: UIApplication) {
3333
}
3434
}
35-

Example/KWVerificationCodeView/VerificationCodeViewController.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ class VerificationCodeViewController: UIViewController {
1414
// MARK: - IBOutlets
1515
@IBOutlet weak var verificationCodeView: KWVerificationCodeView!
1616
@IBOutlet weak var submitButton: UIButton!
17-
17+
1818
// MARK: - Lifecycle
1919
override func viewDidLoad() {
2020
super.viewDidLoad()
21-
21+
2222
submitButton.isEnabled = false
2323
verificationCodeView.delegate = self
2424
}
25-
25+
2626
// MARK: - IBAction
2727
@IBAction func submitButtonTapped(_ sender: UIButton) {
2828
if verificationCodeView.hasValidCode() {

Example/Tests/Tests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,27 @@ import XCTest
33
import KWVerificationCodeView
44

55
class Tests: XCTestCase {
6-
6+
77
override func setUp() {
88
super.setUp()
99
// Put setup code here. This method is called before the invocation of each test method in the class.
1010
}
11-
11+
1212
override func tearDown() {
1313
// Put teardown code here. This method is called after the invocation of each test method in the class.
1414
super.tearDown()
1515
}
16-
16+
1717
func testExample() {
1818
// This is an example of a functional test case.
1919
XCTAssert(true, "Pass")
2020
}
21-
21+
2222
func testPerformanceExample() {
2323
// This is an example of a performance test case.
24-
self.measure() {
24+
self.measure {
2525
// Put the code you want to measure the time of here.
2626
}
2727
}
28-
28+
2929
}

KWVerificationCodeView/Classes/Extensions/StringExtension.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension String {
1212
func trim() -> String {
1313
return trimmingCharacters(in: CharacterSet.whitespaces)
1414
}
15-
15+
1616
func range(from nsRange: NSRange) -> Range<String.Index>? {
1717
guard
1818
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),

KWVerificationCodeView/Classes/KWTextFieldView.swift

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,31 @@ protocol KWTextFieldDelegate: class {
1515
}
1616

1717
@IBDesignable class KWTextFieldView: UIView {
18-
18+
1919
// MARK: - Constants
2020
static let maxCharactersLength = 1
21-
21+
2222
// MARK: - IBInspectables
2323
@IBInspectable var underlineColor: UIColor = UIColor.darkGray {
2424
didSet {
2525
underlineView.backgroundColor = underlineColor
2626
}
2727
}
28-
28+
2929
@IBInspectable var underlineSelectedColor: UIColor = UIColor.black
30-
30+
3131
@IBInspectable var textColor: UIColor = UIColor.darkText {
3232
didSet {
3333
numberTextField.textColor = textColor
3434
}
3535
}
36-
36+
3737
@IBInspectable var textSize: CGFloat = 24.0 {
3838
didSet {
3939
numberTextField.font = UIFont.systemFont(ofSize: textSize)
4040
}
4141
}
42-
42+
4343
@IBInspectable var textFont: String = "" {
4444
didSet {
4545
if let font = UIFont(name: textFont, size: textSize) {
@@ -49,77 +49,82 @@ protocol KWTextFieldDelegate: class {
4949
}
5050
}
5151
}
52-
52+
5353
@IBInspectable var textFieldBackgroundColor: UIColor = UIColor.clear {
5454
didSet {
5555
numberTextField.backgroundColor = textFieldBackgroundColor
5656
}
5757
}
58-
58+
5959
@IBInspectable var textFieldTintColor: UIColor = UIColor.blue {
6060
didSet {
6161
numberTextField.tintColor = textFieldTintColor
6262
}
6363
}
64-
64+
6565
@IBInspectable var darkKeyboard: Bool = false {
6666
didSet {
6767
keyboardAppearance = darkKeyboard ? .dark : .light
6868
numberTextField.keyboardAppearance = keyboardAppearance
6969
}
7070
}
71-
71+
7272
// MARK: - IBOutlets
7373
@IBOutlet weak var numberTextField: UITextField!
7474
@IBOutlet weak private var underlineView: UIView!
75-
75+
7676
// MARK: - Variables
7777
private var keyboardAppearance = UIKeyboardAppearance.default
7878
weak var delegate: KWTextFieldDelegate?
79-
79+
8080
// MARK: - Lifecycle
8181
override init(frame: CGRect) {
8282
super.init(frame: frame)
83-
84-
loadViewFromNib()
83+
84+
setup()
8585
}
86-
86+
8787
required public init?(coder aDecoder: NSCoder) {
8888
super.init(coder: aDecoder)
89-
90-
loadViewFromNib()
91-
numberTextField.delegate = self
92-
NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: numberTextField)
89+
90+
setup()
9391
}
94-
92+
9593
deinit {
9694
NotificationCenter.default.removeObserver(self)
9795
}
98-
96+
97+
// MARK: - Private Methods
98+
private func setup() {
99+
loadViewFromNib()
100+
numberTextField.delegate = self
101+
NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: numberTextField)
102+
}
103+
99104
// MARK: - Public Methods
100105
public func activate() {
101106
numberTextField.becomeFirstResponder()
102-
if numberTextField.text?.characters.count == 0 {
107+
if numberTextField.text?.count == 0 {
103108
numberTextField.text = " "
104109
}
105110
}
106-
111+
107112
public func deactivate() {
108113
numberTextField.resignFirstResponder()
109114
}
110-
115+
111116
public func reset() {
112117
numberTextField.text = " "
113118
updateUnderline()
114119
}
115-
120+
116121
// MARK: - FilePrivate Methods
117122
dynamic fileprivate func textFieldDidChange(_ notification: Foundation.Notification) {
118-
if numberTextField.text?.characters.count == 0 {
123+
if numberTextField.text?.count == 0 {
119124
numberTextField.text = " "
120125
}
121126
}
122-
127+
123128
fileprivate func updateUnderline() {
124129
underlineView.backgroundColor = numberTextField.text?.trim() != "" ? underlineSelectedColor : underlineColor
125130
}
@@ -130,18 +135,18 @@ extension KWTextFieldView: UITextFieldDelegate {
130135
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
131136
let currentString = numberTextField.text!
132137
let newString = currentString.replacingCharacters(in: textField.text!.range(from: range)!, with: string)
133-
134-
if newString.characters.count > type(of: self).maxCharactersLength {
138+
139+
if newString.count > type(of: self).maxCharactersLength {
135140
delegate?.moveToNext(self)
136141
textField.text = string
137-
} else if newString.characters.count == 0 {
142+
} else if newString.count == 0 {
138143
delegate?.moveToPrevious(self, oldCode: textField.text!)
139144
numberTextField.text = " "
140145
}
141-
146+
142147
delegate?.didChangeCharacters()
143148
updateUnderline()
144-
145-
return newString.characters.count <= type(of: self).maxCharactersLength
149+
150+
return newString.count <= type(of: self).maxCharactersLength
146151
}
147152
}

0 commit comments

Comments
 (0)