Skip to content

Commit 30cf278

Browse files
committed
Merged in ui-enhancements (pull request #3)
Fixed issue #3 and added option to set keyboard appearance
2 parents 6c98784 + bd6cc69 commit 30cf278

7 files changed

Lines changed: 145 additions & 29 deletions

File tree

Example/KWVerificationCodeView/Base.lproj/Main.storyboard

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
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" red="1" green="1" blue="1" alpha="1" colorSpace="calibratedRGB"/>
26+
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
2727
<constraints>
2828
<constraint firstAttribute="width" constant="310" id="SmR-e4-qbu"/>
2929
<constraint firstAttribute="height" constant="62" id="mda-A6-NvC"/>
@@ -32,12 +32,6 @@
3232
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
3333
<integer key="value" value="5"/>
3434
</userDefinedRuntimeAttribute>
35-
<userDefinedRuntimeAttribute type="color" keyPath="underlineSelectedColor">
36-
<color key="value" red="0.068236636075229451" green="0.44572890228426398" blue="0.12406174271236946" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
37-
</userDefinedRuntimeAttribute>
38-
<userDefinedRuntimeAttribute type="color" keyPath="underlineColor">
39-
<color key="value" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
40-
</userDefinedRuntimeAttribute>
4135
</userDefinedRuntimeAttributes>
4236
</view>
4337
<button opaque="NO" contentMode="scaleToFill" enabled="NO" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Skh-28-8br" userLabel="submitButton">
@@ -52,7 +46,7 @@
5246
<color key="titleColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
5347
</state>
5448
<state key="disabled">
55-
<color key="titleColor" red="1" green="0.79129829853778799" blue="0.59988017074977373" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
49+
<color key="titleColor" red="1" green="0.7912982985" blue="0.59988017069999999" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
5650
</state>
5751
<userDefinedRuntimeAttributes>
5852
<userDefinedRuntimeAttribute type="number" keyPath="layer.cornerRadius">
@@ -72,11 +66,11 @@
7266
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Enter Code" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="P6k-pN-VEP" userLabel="promptLabel">
7367
<rect key="frame" x="149" y="233" width="77" height="18"/>
7468
<fontDescription key="fontDescription" type="system" pointSize="15"/>
75-
<nil key="textColor"/>
69+
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
7670
<nil key="highlightedColor"/>
7771
</label>
7872
</subviews>
79-
<color key="backgroundColor" red="0.93725490570000003" green="0.93725490570000003" blue="0.95686274770000002" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
73+
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
8074
<constraints>
8175
<constraint firstItem="ekS-Ew-GpT" firstAttribute="top" secondItem="P6k-pN-VEP" secondAttribute="bottom" constant="22" id="H7u-LC-n82"/>
8276
<constraint firstItem="ekS-Ew-GpT" firstAttribute="top" secondItem="6e0-Am-MUv" secondAttribute="bottom" constant="90" id="HQQ-Ne-ux5"/>

KWVerificationCodeView/Classes/KWTextFieldView.swift

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,56 @@ protocol KWTextFieldDelegate: class {
2525
underlineView.backgroundColor = underlineColor
2626
}
2727
}
28-
@IBInspectable var underlineSelectedColor: UIColor = UIColor.blue
28+
29+
@IBInspectable var underlineSelectedColor: UIColor = UIColor.black
30+
31+
@IBInspectable var textColor: UIColor = UIColor.darkText {
32+
didSet {
33+
numberTextField.textColor = textColor
34+
}
35+
}
36+
37+
@IBInspectable var textSize: CGFloat = 24.0 {
38+
didSet {
39+
numberTextField.font = UIFont.systemFont(ofSize: textSize)
40+
}
41+
}
42+
43+
@IBInspectable var textFont: String = "" {
44+
didSet {
45+
if let font = UIFont(name: textFont, size: textSize) {
46+
numberTextField.font = font
47+
} else {
48+
numberTextField.font = UIFont.systemFont(ofSize: textSize)
49+
}
50+
}
51+
}
52+
53+
@IBInspectable var textFieldBackgroundColor: UIColor = UIColor.clear {
54+
didSet {
55+
numberTextField.backgroundColor = textFieldBackgroundColor
56+
}
57+
}
58+
59+
@IBInspectable var textFieldTintColor: UIColor = UIColor.blue {
60+
didSet {
61+
numberTextField.tintColor = textFieldTintColor
62+
}
63+
}
64+
65+
@IBInspectable var darkKeyboard: Bool = false {
66+
didSet {
67+
keyboardAppearance = darkKeyboard ? .dark : .light
68+
numberTextField.keyboardAppearance = keyboardAppearance
69+
}
70+
}
2971

3072
// MARK: - IBOutlets
3173
@IBOutlet weak var numberTextField: UITextField!
3274
@IBOutlet weak private var underlineView: UIView!
3375

3476
// MARK: - Variables
77+
private var keyboardAppearance = UIKeyboardAppearance.default
3578
weak var delegate: KWTextFieldDelegate?
3679

3780
// MARK: - Lifecycle

KWVerificationCodeView/Classes/KWTextFieldView.xib

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@
2020
<rect key="frame" x="0.0" y="0.0" width="50" height="50"/>
2121
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
2222
<subviews>
23-
<textField opaque="NO" clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="uky-3e-m7A" userLabel="numberTextField">
23+
<textField clipsSubviews="YES" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="uky-3e-m7A" userLabel="numberTextField">
2424
<rect key="frame" x="0.0" y="0.0" width="50" height="48"/>
2525
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
26-
<color key="tintColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
26+
<color key="tintColor" red="0.0" green="0.0" blue="1" alpha="1" colorSpace="calibratedRGB"/>
2727
<nil key="textColor"/>
2828
<fontDescription key="fontDescription" type="system" pointSize="24"/>
2929
<textInputTraits key="textInputTraits" keyboardType="numberPad"/>
3030
</textField>
3131
<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"/>
33-
<color key="backgroundColor" red="0.66666666666666663" green="0.66666666666666663" blue="0.66666666666666663" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
33+
<color key="backgroundColor" white="0.33333333333333331" alpha="1" colorSpace="calibratedWhite"/>
3434
<constraints>
3535
<constraint firstAttribute="height" constant="1" id="pFW-zo-tEa"/>
3636
</constraints>

KWVerificationCodeView/Classes/KWVerificationCodeView.swift

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,80 @@ public protocol KWVerificationCodeViewDelegate: class {
2525
}
2626
}
2727
}
28-
@IBInspectable var underlineSelectedColor: UIColor = UIColor.blue {
28+
29+
@IBInspectable var underlineSelectedColor: UIColor = UIColor.black {
2930
didSet {
3031
for textFieldView in textFieldViews {
3132
textFieldView.underlineSelectedColor = underlineSelectedColor
3233
}
3334
}
3435
}
3536

37+
@IBInspectable var textColor: UIColor = UIColor.darkText {
38+
didSet {
39+
for textFieldView in textFieldViews {
40+
textFieldView.numberTextField.textColor = textColor
41+
}
42+
}
43+
}
44+
45+
@IBInspectable var textSize: CGFloat = 24.0 {
46+
didSet {
47+
for textFieldView in textFieldViews {
48+
textFieldView.numberTextField.font = UIFont.systemFont(ofSize: textSize)
49+
}
50+
}
51+
}
52+
53+
@IBInspectable var textFont: String = "" {
54+
didSet {
55+
if let font = UIFont(name: textFont.trim(), size: textSize) {
56+
textFieldFont = font
57+
} else {
58+
textFieldFont = UIFont.systemFont(ofSize: textSize)
59+
}
60+
61+
for textFieldView in textFieldViews {
62+
textFieldView.numberTextField.font = textFieldFont
63+
}
64+
}
65+
}
66+
67+
@IBInspectable var textFieldBackgroundColor: UIColor = UIColor.clear {
68+
didSet {
69+
for textFieldView in textFieldViews {
70+
textFieldView.numberTextField.backgroundColor = textFieldBackgroundColor
71+
}
72+
}
73+
}
74+
75+
@IBInspectable var textFieldTintColor: UIColor = UIColor.blue {
76+
didSet {
77+
for textFieldView in textFieldViews {
78+
textFieldView.numberTextField.tintColor = textFieldTintColor
79+
}
80+
}
81+
}
82+
83+
@IBInspectable var darkKeyboard: Bool = false {
84+
didSet {
85+
keyboardAppearance = darkKeyboard ? .dark : .light
86+
for textFieldView in textFieldViews {
87+
textFieldView.numberTextField.keyboardAppearance = keyboardAppearance
88+
}
89+
}
90+
}
91+
3692
// MARK: - IBOutlets
3793
@IBOutlet weak private var textFieldView1: KWTextFieldView!
3894
@IBOutlet weak private var textFieldView2: KWTextFieldView!
3995
@IBOutlet weak private var textFieldView3: KWTextFieldView!
4096
@IBOutlet weak private var textFieldView4: KWTextFieldView!
4197

4298
// MARK: - Variables
99+
private var keyboardAppearance = UIKeyboardAppearance.default
100+
private var textFieldFont = UIFont.systemFont(ofSize: 24.0)
101+
43102
lazy var textFieldViews: [KWTextFieldView] = {
44103
[unowned self] in
45104

KWVerificationCodeView/Classes/KWVerificationCodeView.xib

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</connections>
1919
</placeholder>
2020
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
21-
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="DUS-Hj-ChZ">
21+
<view userInteractionEnabled="NO" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="DUS-Hj-ChZ">
2222
<rect key="frame" x="0.0" y="0.0" width="375" height="62"/>
2323
<subviews>
2424
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="3tC-Ut-0VN" userLabel="textFieldView1" customClass="KWTextFieldView" customModule="KWVerificationCodeView">
@@ -40,27 +40,19 @@
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 alpha="0.050000000000000003" contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="Uba-Pv-jsf" userLabel="topView">
44-
<rect key="frame" x="0.0" y="0.0" width="375" height="62"/>
45-
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
46-
</view>
4743
</subviews>
4844
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4945
<constraints>
5046
<constraint firstItem="3tC-Ut-0VN" firstAttribute="leading" secondItem="DUS-Hj-ChZ" secondAttribute="leading" constant="10" id="0di-mx-zfw"/>
5147
<constraint firstAttribute="bottom" secondItem="3tC-Ut-0VN" secondAttribute="bottom" constant="6" id="5uF-mn-LoT"/>
52-
<constraint firstItem="Uba-Pv-jsf" firstAttribute="leading" secondItem="DUS-Hj-ChZ" secondAttribute="leading" id="8uT-N2-xM4"/>
5348
<constraint firstItem="jRf-YT-Rjh" firstAttribute="height" secondItem="3tC-Ut-0VN" secondAttribute="height" id="Gb4-Os-cwr"/>
5449
<constraint firstItem="vt9-dd-ekk" firstAttribute="top" secondItem="DUS-Hj-ChZ" secondAttribute="top" constant="6" id="Kg2-dv-3YJ"/>
5550
<constraint firstItem="jRf-YT-Rjh" firstAttribute="width" secondItem="3tC-Ut-0VN" secondAttribute="width" id="U4v-Ht-Drn"/>
5651
<constraint firstItem="vt9-dd-ekk" firstAttribute="width" secondItem="uqQ-2Z-1XV" secondAttribute="width" id="VK2-dB-JL0"/>
57-
<constraint firstAttribute="bottom" secondItem="Uba-Pv-jsf" secondAttribute="bottom" id="VpP-Ye-4LD"/>
5852
<constraint firstItem="jRf-YT-Rjh" firstAttribute="top" secondItem="DUS-Hj-ChZ" secondAttribute="top" constant="6" id="Vvq-kp-ykE"/>
5953
<constraint firstAttribute="trailing" secondItem="vt9-dd-ekk" secondAttribute="trailing" constant="10" id="fPo-e5-fRn"/>
60-
<constraint firstItem="Uba-Pv-jsf" firstAttribute="top" secondItem="DUS-Hj-ChZ" secondAttribute="top" id="gfu-mb-7SY"/>
6154
<constraint firstItem="vt9-dd-ekk" firstAttribute="height" secondItem="uqQ-2Z-1XV" secondAttribute="height" id="jCR-aZ-KYb"/>
6255
<constraint firstItem="uqQ-2Z-1XV" firstAttribute="height" secondItem="jRf-YT-Rjh" secondAttribute="height" id="nlt-Sw-o2S"/>
63-
<constraint firstAttribute="trailing" secondItem="Uba-Pv-jsf" secondAttribute="trailing" id="q5B-Lv-1ay"/>
6456
<constraint firstItem="uqQ-2Z-1XV" firstAttribute="leading" secondItem="jRf-YT-Rjh" secondAttribute="trailing" constant="10" id="rEH-pO-Tk3"/>
6557
<constraint firstItem="uqQ-2Z-1XV" firstAttribute="width" secondItem="jRf-YT-Rjh" secondAttribute="width" id="rtg-SH-V0Y"/>
6658
<constraint firstItem="vt9-dd-ekk" firstAttribute="leading" secondItem="uqQ-2Z-1XV" secondAttribute="trailing" constant="10" id="u6T-vB-S9e"/>

README.md

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,45 @@ Add a `UIView` in your *Storyboard* and change the class to `KWVerificationCodeV
3131

3232
![Interface Builder Screenshot](Screenshots/interfacebuilder.png)
3333

34-
It is possile to set the default and selected colors of the underline in the *Storyboard*.
34+
The properties which can be directly set in the *Attributes Inspector* and their default values are as follows:
35+
36+
- `Underline Color` - Dark grey color
37+
38+
- `Underline Selected Color` - Black color
39+
40+
- `Text Color` - Dark text color
41+
42+
- `Text Size` - 24.0
43+
44+
- `Text Font` - System font
45+
46+
- `Text Field Background Color` - Clear color
47+
48+
- `Text Field Tint Color` - Blue color
49+
50+
- `Dark Keyboard` - Default keyboard apperance
3551

3652
### Methods
3753

38-
`hasValidCode() -> Bool` - Returns true when the entered code is valid.
54+
The `KWVerificationCodeView` has the following methods:
55+
56+
hasValidCode() -> Bool
57+
58+
Returns true when the entered code is valid.
59+
60+
getVerificationCode() -> String
61+
62+
Returns the validation code.
63+
64+
### Protocols
65+
66+
#### KWVerificationCodeViewDelegate
3967

40-
`getVerificationCode() -> String` - Returns the validation code.
68+
KWVerificationCodeViewDelegate protocol has the following method:
4169

42-
### Delegate
70+
didChangeVerificationCode()
4371

44-
`KWVerificationCodeViewDelegate` has a method `didChangeVerificationCode()`, which you can implement to check for valid code in real time. This comes handy in stituations where you have to enable the submit button only if the verification code is valid.
72+
Notifies that the text in `KWVerificationCodeView` has been changed. This is especially useful in situations where you have to enable the submit button only if the verification code is valid.
4573

4674
## Author
4775

Screenshots/interfacebuilder.png

33.7 KB
Loading

0 commit comments

Comments
 (0)