Skip to content

Commit 97879a7

Browse files
author
Athul Sai
committed
Added new range string extension and fixed spacing issues
1 parent b34cc9f commit 97879a7

4 files changed

Lines changed: 25 additions & 17 deletions

File tree

Example/KWVerificationCodeView/Base.lproj/LaunchScreen.xib

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,17 @@
1616
<rect key="frame" x="0.0" y="0.0" width="480" height="480"/>
1717
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1818
<subviews>
19-
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text=" Copyright (c) 2017 Keep Works. All rights reserved." textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" minimumFontSize="9" translatesAutoresizingMaskIntoConstraints="NO" id="8ie-xW-0ye" userLabel="copyrightLabel">
20-
<rect key="frame" x="20" y="439" width="441" height="21"/>
21-
<fontDescription key="fontDescription" type="system" pointSize="17"/>
22-
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
23-
<nil key="highlightedColor"/>
24-
</label>
2519
<label opaque="NO" clipsSubviews="YES" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="KWVerificationCodeView" textAlignment="center" lineBreakMode="middleTruncation" baselineAdjustment="alignBaselines" minimumFontSize="18" translatesAutoresizingMaskIntoConstraints="NO" id="kId-c2-rCX" userLabel="titleLabel">
26-
<rect key="frame" x="20" y="140" width="441" height="43"/>
20+
<rect key="frame" x="20" y="138.5" width="441" height="43"/>
2721
<fontDescription key="fontDescription" type="boldSystem" pointSize="36"/>
2822
<color key="textColor" cocoaTouchSystemColor="darkTextColor"/>
2923
<nil key="highlightedColor"/>
3024
</label>
3125
</subviews>
3226
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3327
<constraints>
34-
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" constant="1" id="5cJ-9S-tgC"/>
28+
<constraint firstItem="kId-c2-rCX" firstAttribute="centerY" secondItem="iN0-l3-epB" secondAttribute="bottom" multiplier="1/3" id="5cJ-9S-tgC"/>
3529
<constraint firstAttribute="centerX" secondItem="kId-c2-rCX" secondAttribute="centerX" id="Koa-jz-hwk"/>
36-
<constraint firstAttribute="bottom" secondItem="8ie-xW-0ye" secondAttribute="bottom" constant="20" id="Kzo-t9-V3l"/>
37-
<constraint firstItem="8ie-xW-0ye" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="MfP-vx-nX0"/>
38-
<constraint firstAttribute="centerX" secondItem="8ie-xW-0ye" secondAttribute="centerX" id="ZEH-qu-HZ9"/>
3930
<constraint firstItem="kId-c2-rCX" firstAttribute="leading" secondItem="iN0-l3-epB" secondAttribute="leading" constant="20" symbolic="YES" id="fvb-Df-36g"/>
4031
</constraints>
4132
<nil key="simulatedStatusBarMetrics"/>

Example/KWVerificationCodeView/Base.lproj/Main.storyboard

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@
4444
<rect key="frame" x="220" y="6" width="50" height="50"/>
4545
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
4646
</view>
47+
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="IIA-q0-jPG" userLabel="topView">
48+
<rect key="frame" x="0.0" y="0.0" width="310" height="62"/>
49+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="calibratedWhite"/>
50+
</view>
4751
</subviews>
4852
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
4953
<constraints>
@@ -54,8 +58,10 @@
5458
<constraint firstItem="jCB-ix-jVy" firstAttribute="height" secondItem="4Am-VB-b0X" secondAttribute="height" id="H6Z-iL-Gby"/>
5559
<constraint firstItem="jCB-ix-jVy" firstAttribute="leading" secondItem="4Am-VB-b0X" secondAttribute="trailing" constant="10" id="LUP-1p-GKY"/>
5660
<constraint firstItem="jCB-ix-jVy" firstAttribute="width" secondItem="4Am-VB-b0X" secondAttribute="width" id="Oo3-Mx-syM"/>
61+
<constraint firstAttribute="bottom" secondItem="IIA-q0-jPG" secondAttribute="bottom" id="QlT-T1-yte"/>
5762
<constraint firstItem="4Am-VB-b0X" firstAttribute="height" secondItem="KnL-ww-PSX" secondAttribute="height" id="Sht-Ab-1NM"/>
5863
<constraint firstAttribute="width" constant="310" id="SmR-e4-qbu"/>
64+
<constraint firstItem="IIA-q0-jPG" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" id="TDb-iM-ynn"/>
5965
<constraint firstItem="KnL-ww-PSX" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" constant="6" id="YIt-VA-9pj"/>
6066
<constraint firstItem="KnL-ww-PSX" firstAttribute="width" secondItem="pAW-9I-Grs" secondAttribute="width" id="af2-o8-CgB"/>
6167
<constraint firstAttribute="bottom" secondItem="pAW-9I-Grs" secondAttribute="bottom" constant="6" id="cr1-oF-szO"/>
@@ -64,6 +70,8 @@
6470
<constraint firstItem="4Am-VB-b0X" firstAttribute="width" secondItem="KnL-ww-PSX" secondAttribute="width" id="qK6-Of-XBI"/>
6571
<constraint firstItem="pAW-9I-Grs" firstAttribute="top" secondItem="ekS-Ew-GpT" secondAttribute="top" constant="6" id="tPY-XH-pWS"/>
6672
<constraint firstItem="pAW-9I-Grs" firstAttribute="leading" secondItem="ekS-Ew-GpT" secondAttribute="leading" constant="40" id="vJb-5J-83w"/>
73+
<constraint firstAttribute="trailing" secondItem="IIA-q0-jPG" secondAttribute="trailing" id="w1L-mH-VZy"/>
74+
<constraint firstItem="IIA-q0-jPG" firstAttribute="leading" secondItem="ekS-Ew-GpT" secondAttribute="leading" id="y1n-FM-Zsl"/>
6775
</constraints>
6876
</view>
6977
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="Skh-28-8br" userLabel="submitButton">

KWVerificationCodeView/Classes/Extensions/StringExtension.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,14 @@ extension String {
1212
func trim() -> String {
1313
return trimmingCharacters(in: CharacterSet.whitespaces)
1414
}
15+
16+
func range(from nsRange: NSRange) -> Range<String.Index>? {
17+
guard
18+
let from16 = utf16.index(utf16.startIndex, offsetBy: nsRange.location, limitedBy: utf16.endIndex),
19+
let to16 = utf16.index(from16, offsetBy: nsRange.length, limitedBy: utf16.endIndex),
20+
let from = String.Index(from16, within: self),
21+
let to = String.Index(to16, within: self)
22+
else { return nil }
23+
return from ..< to
24+
}
1525
}

KWVerificationCodeView/Classes/KWVerificationCodeView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public protocol KWVerificationCodeDelegate: class {
1313
func moveToPrevious(_ verificationCodeView: KWVerificationCodeView, oldCode: String)
1414
}
1515

16-
1716
@IBDesignable open class KWVerificationCodeView: UIView {
1817

1918
// MARK: - Constants
@@ -84,19 +83,19 @@ public protocol KWVerificationCodeDelegate: class {
8483
// MARK: - UITextFieldDelegate
8584
extension KWVerificationCodeView: UITextFieldDelegate {
8685
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
87-
let currentString: NSString = numberTextField.text! as NSString
88-
let newString: NSString = currentString.replacingCharacters(in: range, with: string) as NSString
86+
let currentString = numberTextField.text!
87+
let newString = currentString.replacingCharacters(in: textField.text!.range(from: range)!, with: string)
8988

90-
if newString.length > type(of: self).maxCharactersLength {
89+
if newString.characters.count > type(of: self).maxCharactersLength {
9190
delegate?.moveToNext(self)
9291
textField.text = string
93-
} else if newString.length == 0 {
92+
} else if newString.characters.count == 0 {
9493
delegate?.moveToPrevious(self, oldCode: textField.text!)
9594
numberTextField.text = " "
9695
}
9796

9897
updateUnderline()
9998

100-
return newString.length <= type(of: self).maxCharactersLength
99+
return newString.characters.count <= type(of: self).maxCharactersLength
101100
}
102101
}

0 commit comments

Comments
 (0)