-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in verification-module (pull request #2)
Resolves issue #2
- Loading branch information
Showing
14 changed files
with
376 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 37 additions & 62 deletions
99
Example/KWVerificationCodeView/Base.lproj/Main.storyboard
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
Pod::Spec.new do |s| | ||
s.name = 'KWVerificationCodeView' | ||
s.version = '0.1.0' | ||
s.summary = 'A subclass on UIView that displays a verification code field.' | ||
s.description = 'This CocoaPod provides the ability to use a UIView that can display a verification field.' | ||
s.homepage = 'https://bitbucket.org/keepworks/kwverificationcodeview' | ||
s.summary = 'A verification code view with validation.' | ||
s.description = 'A customisable verification code view with built in validation. Can be used for one time passwords (OTPs), email verification codes etc.' | ||
s.homepage = 'https://github.com/keepworks/kwverificationcodeview' | ||
s.license = { :type => 'MIT', :file => 'LICENSE' } | ||
s.author = { 'KeepWorks' => '[email protected]' } | ||
s.source = { :git => 'https://bitbucket.org/keepworks/kwverificationcodeview.git', :tag => s.version.to_s } | ||
s.source = { :git => 'https://github.com/keepworks/kwverificationcodeview.git', :tag => s.version.to_s } | ||
|
||
s.ios.deployment_target = '8.0' | ||
s.source_files = 'KWVerificationCodeView/Classes/**/*' | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// | ||
// KWTextFieldView.swift | ||
// Pods | ||
// | ||
// Created by KeepWorks on 13/01/17. | ||
// Copyright © 2017 KeepWorks Technologies Pvt Ltd. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
protocol KWTextFieldDelegate: class { | ||
func moveToNext(_ textFieldView: KWTextFieldView) | ||
func moveToPrevious(_ textFieldView: KWTextFieldView, oldCode: String) | ||
func didChangeCharacters() | ||
} | ||
|
||
@IBDesignable class KWTextFieldView: UIView { | ||
|
||
// MARK: - Constants | ||
static let maxCharactersLength = 1 | ||
|
||
// MARK: - IBInspectables | ||
@IBInspectable var underlineColor: UIColor = UIColor.darkGray { | ||
didSet { | ||
underlineView.backgroundColor = underlineColor | ||
} | ||
} | ||
@IBInspectable var underlineSelectedColor: UIColor = UIColor.blue | ||
|
||
// MARK: - IBOutlets | ||
@IBOutlet weak var numberTextField: UITextField! | ||
@IBOutlet weak private var underlineView: UIView! | ||
|
||
// MARK: - Variables | ||
weak var delegate: KWTextFieldDelegate? | ||
|
||
// MARK: - Lifecycle | ||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
|
||
loadViewFromNib() | ||
} | ||
|
||
required public init?(coder aDecoder: NSCoder) { | ||
super.init(coder: aDecoder) | ||
|
||
loadViewFromNib() | ||
numberTextField.delegate = self | ||
NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: numberTextField) | ||
} | ||
|
||
deinit { | ||
NotificationCenter.default.removeObserver(self) | ||
} | ||
|
||
// MARK: - Public Methods | ||
public func activate() { | ||
numberTextField.becomeFirstResponder() | ||
if numberTextField.text?.characters.count == 0 { | ||
numberTextField.text = " " | ||
} | ||
} | ||
|
||
public func deactivate() { | ||
numberTextField.resignFirstResponder() | ||
} | ||
|
||
public func reset() { | ||
numberTextField.text = " " | ||
updateUnderline() | ||
} | ||
|
||
// MARK: - FilePrivate Methods | ||
dynamic fileprivate func textFieldDidChange(_ notification: Foundation.Notification) { | ||
if numberTextField.text?.characters.count == 0 { | ||
numberTextField.text = " " | ||
} | ||
} | ||
|
||
fileprivate func updateUnderline() { | ||
underlineView.backgroundColor = numberTextField.text?.trim() != "" ? underlineSelectedColor : underlineColor | ||
} | ||
} | ||
|
||
// MARK: - UITextFieldDelegate | ||
extension KWTextFieldView: UITextFieldDelegate { | ||
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool { | ||
let currentString = numberTextField.text! | ||
let newString = currentString.replacingCharacters(in: textField.text!.range(from: range)!, with: string) | ||
|
||
if newString.characters.count > type(of: self).maxCharactersLength { | ||
delegate?.moveToNext(self) | ||
textField.text = string | ||
} else if newString.characters.count == 0 { | ||
delegate?.moveToPrevious(self, oldCode: textField.text!) | ||
numberTextField.text = " " | ||
} | ||
|
||
delegate?.didChangeCharacters() | ||
updateUnderline() | ||
|
||
return newString.characters.count <= type(of: self).maxCharactersLength | ||
} | ||
} |
Oops, something went wrong.