Skip to content

Commit 21f3640

Browse files
committed
Update
1 parent b35a4e4 commit 21f3640

File tree

3 files changed

+57
-58
lines changed

3 files changed

+57
-58
lines changed

Diff for: KAPinField/Example/Base.lproj/Main.storyboard

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17156" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="21507" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
33
<device id="retina4_0" orientation="portrait" appearance="light"/>
44
<dependencies>
55
<deployment identifier="iOS"/>
6-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
6+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
77
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
88
<capability name="System colors in document resources" minToolsVersion="11.0"/>
99
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
@@ -34,6 +34,9 @@
3434
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
3535
<color key="onTintColor" red="0.094117647060000004" green="0.5450980392" blue="0.96078431369999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
3636
<color key="thumbTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
37+
<connections>
38+
<action selector="toggleSecure" destination="BYZ-38-t0r" eventType="valueChanged" id="ptC-yn-G11"/>
39+
</connections>
3740
</switch>
3841
</subviews>
3942
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
@@ -57,15 +60,15 @@
5760
</constraints>
5861
</view>
5962
<label opaque="NO" alpha="0.5" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" verticalCompressionResistancePriority="1000" text="Code : 1234" textAlignment="center" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Ee6-G1-KAi">
60-
<rect key="frame" x="121" y="157" width="78" height="17"/>
63+
<rect key="frame" x="121" y="145" width="78" height="17"/>
6164
<constraints>
6265
<constraint firstAttribute="height" constant="17" id="xM1-qL-uy3"/>
6366
</constraints>
6467
<fontDescription key="fontDescription" type="system" pointSize="14"/>
6568
<nil key="highlightedColor"/>
6669
</label>
6770
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZrA-JK-GPg" userLabel="embedViewTest">
68-
<rect key="frame" x="0.0" y="194" width="320" height="50"/>
71+
<rect key="frame" x="0.0" y="182" width="320" height="50"/>
6972
<subviews>
7073
<textField opaque="NO" contentMode="scaleToFill" horizontalCompressionResistancePriority="250" verticalCompressionResistancePriority="250" contentHorizontalAlignment="left" contentVerticalAlignment="center" text="KAPinField" textAlignment="center" minimumFontSize="17" translatesAutoresizingMaskIntoConstraints="NO" id="d8l-Ws-A8U" customClass="KAPinField" customModule="KAPinField" customModuleProvider="target">
7174
<rect key="frame" x="18" y="0.0" width="284" height="50"/>
@@ -86,7 +89,7 @@
8689
</view>
8790
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="deI-wI-pgE">
8891
<rect key="frame" x="92" y="264" width="136" height="40"/>
89-
<color key="backgroundColor" white="1" alpha="0.14506635273972604" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
92+
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
9093
<constraints>
9194
<constraint firstAttribute="height" constant="40" id="o02-23-GNN"/>
9295
</constraints>
@@ -96,14 +99,14 @@
9699
<color key="titleColor" systemColor="systemBlueColor"/>
97100
</state>
98101
<connections>
99-
<action selector="refreshPinField" destination="BYZ-38-t0r" eventType="touchUpInside" id="6Le-gZ-Rl0"/>
102+
<action selector="setupPinfield" destination="BYZ-38-t0r" eventType="touchUpInside" id="q70-6w-eig"/>
100103
</connections>
101104
</button>
102105
</subviews>
103106
<viewLayoutGuide key="safeArea" id="aDb-oH-ANJ"/>
104107
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
105108
<constraints>
106-
<constraint firstItem="deI-wI-pgE" firstAttribute="top" secondItem="ZrA-JK-GPg" secondAttribute="bottom" constant="20" id="7CR-QR-oP5"/>
109+
<constraint firstItem="deI-wI-pgE" firstAttribute="top" secondItem="ZrA-JK-GPg" secondAttribute="bottom" constant="32" id="7CR-QR-oP5"/>
107110
<constraint firstItem="deI-wI-pgE" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="8PS-0h-axA"/>
108111
<constraint firstItem="Ee6-G1-KAi" firstAttribute="centerX" secondItem="aDb-oH-ANJ" secondAttribute="centerX" id="8wt-lz-a2B"/>
109112
<constraint firstItem="aDb-oH-ANJ" firstAttribute="top" secondItem="u2o-qm-AuS" secondAttribute="top" id="9ha-nD-VAo"/>

Diff for: KAPinField/Example/ViewController.swift

+40-38
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,11 @@ class ViewController: UIViewController {
2424
return .lightContent
2525
}
2626

27-
28-
2927
override func viewDidLoad() {
3028
super.viewDidLoad()
3129

32-
// -- Appearance --
33-
self.updateStyle()
34-
3530
// -- Properties --
36-
pinField.properties.delegate = self
37-
self.refreshPinField()
31+
self.setupPinfield()
3832

3933
// Get focus
4034
pinField.becomeFirstResponder()
@@ -49,50 +43,58 @@ class ViewController: UIViewController {
4943
return string
5044
}
5145

52-
@IBAction func refreshPinField() {
46+
@IBAction func setupPinfield() {
5347

5448
// Random numberOfCharacters
5549
pinField.text = ""
56-
pinField.properties.numberOfCharacters = [4,5].randomElement()!
50+
51+
pinField.updateProperties { properties in
52+
properties.numberOfCharacters = [4,5].randomElement()!
53+
properties.delegate = self
54+
}
5755

5856
// Random target code
5957
targetCode = self.randomCode(numDigits: pinField.properties.numberOfCharacters)
6058
targetCodeLabel.text = "Code : \(targetCode)"
6159
UIPasteboard.general.string = targetCode
6260

63-
self.updateStyle()
61+
self.refresh()
6462
}
6563

66-
func updateStyle() {
67-
68-
pinField.properties.isSecure = self.secureSwitch.isOn
69-
64+
@IBAction func toggleSecure() {
65+
self.refresh()
66+
self.pinField.becomeFirstResponder()
67+
}
68+
69+
func refresh() {
70+
7071
self.targetCodeLabel.textColor = UIColor.label.withAlphaComponent(0.8)
7172

72-
pinField.properties.token = "-"
73-
pinField.properties.animateFocus = false
74-
pinField.properties.isUppercased = false
75-
pinField.properties.validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
76-
77-
// let startIndex = self.targetCode.index(self.targetCode.startIndex, offsetBy: 0)
78-
// let endIndex = self.targetCode.index(self.targetCode.startIndex, offsetBy: 1)
79-
// pinField.text = String(self.targetCode[startIndex...endIndex])
73+
pinField.updateProperties { properties in
74+
properties.isSecure = self.secureSwitch.isOn
75+
properties.token = ""
76+
properties.animateFocus = false
77+
properties.isUppercased = false
78+
properties.validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
79+
}
8080

81-
pinField.appearance.tokenColor = UIColor.label.withAlphaComponent(0.2)
82-
pinField.appearance.tokenFocusColor = UIColor.label.withAlphaComponent(0.2)
83-
pinField.appearance.textColor = UIColor.label
84-
pinField.appearance.font = .courierBold(40)
85-
pinField.appearance.kerning = 24
86-
pinField.appearance.backOffset = 5
87-
pinField.appearance.backColor = UIColor.clear
88-
pinField.appearance.backBorderWidth = 1
89-
pinField.appearance.backBorderColor = UIColor.label.withAlphaComponent(0.2)
90-
pinField.appearance.backCornerRadius = 4
91-
pinField.appearance.backFocusColor = UIColor.clear
92-
pinField.appearance.backBorderFocusColor = UIColor.label.withAlphaComponent(0.8)
93-
pinField.appearance.backActiveColor = UIColor.clear
94-
pinField.appearance.backBorderActiveColor = UIColor.label
95-
pinField.appearance.backRounded = false
81+
pinField.updateAppearence { appearance in
82+
appearance.tokenColor = UIColor.clear
83+
appearance.tokenFocusColor = UIColor.clear
84+
appearance.textColor = UIColor.label
85+
appearance.font = .menlo(40)
86+
appearance.kerning = 24
87+
appearance.backOffset = 8
88+
appearance.backColor = UIColor.clear
89+
appearance.backBorderWidth = 1
90+
appearance.backBorderColor = UIColor.label.withAlphaComponent(0.2)
91+
appearance.backCornerRadius = 4
92+
appearance.backFocusColor = UIColor.clear
93+
appearance.backBorderFocusColor = UIColor.systemBlue
94+
appearance.backActiveColor = UIColor.clear
95+
appearance.backBorderActiveColor = UIColor.label
96+
appearance.backRounded = false
97+
}
9698
}
9799
}
98100

@@ -117,7 +119,7 @@ extension ViewController : KAPinFieldDelegate {
117119
} else {
118120
print("Success")
119121
field.animateSuccess(with: "👍") {
120-
self.refreshPinField()
122+
self.setupPinfield()
121123
}
122124

123125
}

Diff for: KAPinField/Sources/KAPinField.swift

+7-13
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@ public struct KAPinFieldProperties {
3434
public var token: Character = "" {
3535
didSet {
3636
precondition(!validCharacters.contains(token), "🚫 token can't be one of the valid characters \"\(token)\"")
37-
38-
// Change space to insecable space
39-
if token == " " {
40-
self.token = " "
41-
}
37+
precondition(!token.isWhitespace, "🚫 token can't be a whitespace. Please use a token with a clear color to achieve the same effect")
4238
}
4339
}
4440
public var animateFocus : Bool = true
@@ -189,7 +185,6 @@ public class KAPinField : UITextField {
189185
}
190186

191187
// back views
192-
193188
var myText = ""
194189
for _ in 0..<self.properties.numberOfCharacters {
195190
myText += "0"
@@ -419,7 +414,6 @@ public class KAPinField : UITextField {
419414
let duration: Double = isClear ? 0.3 : 0.6
420415
if isClear{
421416
atts[.foregroundColor] = self.appearance.tokenFocusColor
422-
?? self.appearance.tokenColor
423417
} else {
424418
atts[.foregroundColor] = UIColor.clear
425419
}
@@ -450,9 +444,9 @@ public class KAPinField : UITextField {
450444

451445
self.sanitizeText()
452446

453-
454447
let paragraph = NSMutableParagraphStyle()
455448
paragraph.alignment = .center
449+
456450
let font = self.appearance.font?.font() ?? self.font ?? UIFont.preferredFont(forTextStyle: .headline)
457451
self.attributes = [ .paragraphStyle : paragraph,
458452
.font : font,
@@ -497,8 +491,8 @@ public class KAPinField : UITextField {
497491
backView.layer.borderColor = self.appearance.backBorderColor.cgColor
498492
} else {
499493
attributes[.foregroundColor] = self.appearance.textColor
500-
backView.backgroundColor = self.appearance.backActiveColor ?? self.appearance.backColor
501-
backView.layer.borderColor = self.appearance.backBorderActiveColor.cgColor ?? self.appearance.backBorderColor.cgColor
494+
backView.backgroundColor = self.appearance.backActiveColor
495+
backView.layer.borderColor = self.appearance.backBorderActiveColor.cgColor
502496
}
503497
}
504498

@@ -507,6 +501,7 @@ public class KAPinField : UITextField {
507501
if i == indexForKernFix {
508502
attributes[.kern] = 0.0
509503
}
504+
510505
attString.append(NSAttributedString(string: string, attributes: attributes))
511506
}
512507

@@ -588,7 +583,6 @@ public class KAPinField : UITextField {
588583
// Token focus color
589584
var atts = attString.attributes(at: range.location, effectiveRange: nil)
590585
atts[.foregroundColor] = self.appearance.tokenFocusColor
591-
?? self.appearance.tokenColor
592586
attString.setAttributes(atts, range: range)
593587

594588
// Avoid long fade from tick()
@@ -604,8 +598,8 @@ public class KAPinField : UITextField {
604598
backIndex = max(backIndex, 0)
605599
if !self.backViews.isEmpty && backIndex < self.backViews.count {
606600
let backView = self.backViews[backIndex]
607-
backView.backgroundColor = self.appearance.backFocusColor ?? self.appearance.backColor
608-
backView.layer.borderColor = self.appearance.backBorderFocusColor.cgColor ?? self.appearance.backBorderColor.cgColor
601+
backView.backgroundColor = self.appearance.backFocusColor
602+
backView.layer.borderColor = self.appearance.backBorderFocusColor.cgColor
609603
}
610604
}
611605
}

0 commit comments

Comments
 (0)