Skip to content

Commit 263c789

Browse files
author
Athul Sai
committed
Fixed issue #2 and added option to set underline selected color
1 parent bc657f4 commit 263c789

6 files changed

Lines changed: 29 additions & 36 deletions

File tree

Example/KWVerificationCodeView/Base.lproj/Main.storyboard

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<subviews>
2424
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ekS-Ew-GpT" userLabel="verificationCodeView" customClass="KWVerificationCodeView" customModule="KWVerificationCodeView">
2525
<rect key="frame" x="33" y="273" width="310" height="62"/>
26-
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
2726
<constraints>
2827
<constraint firstAttribute="width" constant="310" id="SmR-e4-qbu"/>
2928
<constraint firstAttribute="height" constant="62" id="mda-A6-NvC"/>

Example/KWVerificationCodeView/VerificationCodeViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class VerificationCodeViewController: UIViewController {
2121

2222
// MARK: - IBAction
2323
@IBAction func submitButtonTapped(_ sender: UIButton) {
24-
if verificationCodeView.validateCode() {
24+
if verificationCodeView.hasValidCode() {
2525
let alertController = UIAlertController(title: "Success", message: "Code is \(verificationCodeView.getVerificationCode())", preferredStyle: .alert)
2626
let okAction = UIAlertAction(title: "Ok", style: .default, handler: nil)
2727
alertController.addAction(okAction)

KWVerificationCodeView/Classes/KWTextFieldView.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ protocol KWTextFieldDelegate: class {
2121
// MARK: - IBInspectables
2222
@IBInspectable var underlineColor: UIColor = UIColor.darkGray {
2323
didSet {
24-
underlineView.backgroundColor = self.underlineColor.withAlphaComponent(0.3)
24+
underlineView.backgroundColor = self.underlineColor
2525
}
2626
}
27+
@IBInspectable var underlineSelectedColor: UIColor = UIColor.blue
2728

2829
// MARK: - IBOutlets
2930
@IBOutlet weak var numberTextField: UITextField!
30-
@IBOutlet weak var underlineView: UIView!
31+
@IBOutlet weak private var underlineView: UIView!
3132

3233
// MARK: - Variables
3334
weak var delegate: KWTextFieldDelegate?
@@ -76,7 +77,7 @@ protocol KWTextFieldDelegate: class {
7677
}
7778

7879
fileprivate func updateUnderline() {
79-
underlineView.backgroundColor = numberTextField.text?.trim() != "" ? self.underlineColor : self.underlineColor.withAlphaComponent(0.3)
80+
underlineView.backgroundColor = numberTextField.text?.trim() != "" ? self.underlineSelectedColor : self.underlineColor
8081
}
8182
}
8283

KWVerificationCodeView/Classes/KWTextFieldView.xib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
<fontDescription key="fontDescription" type="system" pointSize="24"/>
2929
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
3030
</textField>
31-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="eC6-EU-k8d" userLabel="underlineView">
31+
<view userInteractionEnabled="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="eC6-EU-k8d" userLabel="underlineView">
3232
<rect key="frame" x="0.0" y="39" width="50" height="1"/>
3333
<color key="backgroundColor" red="0.66666666666666663" green="0.66666666666666663" blue="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3434
<constraints>

KWVerificationCodeView/Classes/KWVerificationCodeView.swift

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,52 @@
88

99
import UIKit
1010

11-
//public protocol KWVerificationCodeDelegate: class {
12-
// func getVerificationCode() -> String
13-
// func validateCode() -> Bool
14-
//}
15-
1611
@IBDesignable open class KWVerificationCodeView: UIView {
1712

1813
// MARK: - Constants
1914
static let maxCharactersLength = 1
2015

2116
// MARK: - IBInspectables
22-
//@IBInspectable open var underlineColor: UIColor = UIColor.darkGray {
23-
// didSet {
24-
// underlineView.backgroundColor = self.underlineColor.withAlphaComponent(0.3)
25-
// }
26-
//}
17+
@IBInspectable open var underlineColor: UIColor = UIColor.darkGray {
18+
didSet {
19+
for textFieldView in textFieldViews {
20+
textFieldView.underlineColor = self.underlineColor
21+
}
22+
}
23+
}
24+
@IBInspectable var underlineSelectedColor: UIColor = UIColor.blue {
25+
didSet {
26+
for textFieldView in textFieldViews {
27+
textFieldView.underlineSelectedColor = self.underlineSelectedColor
28+
}
29+
}
30+
}
2731

2832
// MARK: - IBOutlets
29-
@IBOutlet weak var textFieldView1: KWTextFieldView!
30-
@IBOutlet weak var textFieldView2: KWTextFieldView!
31-
@IBOutlet weak var textFieldView3: KWTextFieldView!
32-
@IBOutlet weak var textFieldView4: KWTextFieldView!
33+
@IBOutlet weak private var textFieldView1: KWTextFieldView!
34+
@IBOutlet weak private var textFieldView2: KWTextFieldView!
35+
@IBOutlet weak private var textFieldView3: KWTextFieldView!
36+
@IBOutlet weak private var textFieldView4: KWTextFieldView!
3337

3438
// MARK: - Variables
35-
var mobile: String!
36-
var selectedVerificationCodeViewIndex = 0
37-
3839
lazy var textFieldViews: [KWTextFieldView] = {
3940
[unowned self] in
4041

4142
return [self.textFieldView1, self.textFieldView2, self.textFieldView3, self.textFieldView4]
4243
}()
43-
44-
//weak public var delegate: KWVerificationCodeDelegate?
4544

4645
// MARK: - Lifecycle
4746
override init(frame: CGRect) {
4847
super.init(frame: frame)
4948

5049
loadViewFromNib()
5150
}
52-
51+
5352
required public init?(coder aDecoder: NSCoder) {
5453
super.init(coder: aDecoder)
5554

5655
loadViewFromNib()
5756
setupVerificationCodeViews()
58-
//numberTextField.delegate = self
59-
//NotificationCenter.default.addObserver(self, selector: #selector(textFieldDidChange(_:)), name: NSNotification.Name.UITextFieldTextDidChange, object: numberTextField)
60-
}
61-
62-
deinit {
63-
//NotificationCenter.default.removeObserver(self)
6457
}
6558

6659
// MARK: - Public Methods
@@ -73,7 +66,7 @@ import UIKit
7366
return verificationCode
7467
}
7568

76-
public func validateCode() -> Bool {
69+
public func hasValidCode() -> Bool {
7770
for textFieldView in textFieldViews {
7871
if Int(textFieldView.numberTextField.text!) == nil {
7972
return false
@@ -82,7 +75,7 @@ import UIKit
8275

8376
return true
8477
}
85-
78+
8679
// MARK: - Private Methods
8780
private func setupVerificationCodeViews() {
8881
for textFieldView in textFieldViews {

KWVerificationCodeView/Classes/KWVerificationCodeView.xib

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@
4040
<rect key="frame" x="284" y="6" width="81" height="50"/>
4141
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4242
</view>
43-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Uba-Pv-jsf" userLabel="topView">
43+
<view alpha="0.050000000000000003" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Uba-Pv-jsf" userLabel="topView">
4444
<rect key="frame" x="0.0" y="0.0" width="375" height="62"/>
4545
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4646
</view>
4747
</subviews>
48-
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
48+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4949
<constraints>
5050
<constraint firstItem="3tC-Ut-0VN" firstAttribute="leading" secondItem="DUS-Hj-ChZ" secondAttribute="leading" constant="10" id="0di-mx-zfw"/>
5151
<constraint firstAttribute="bottom" secondItem="3tC-Ut-0VN" secondAttribute="bottom" constant="6" id="5uF-mn-LoT"/>

0 commit comments

Comments
 (0)