Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
alexiscreuzot committed Jan 16, 2023
1 parent b35a4e4 commit 21f3640
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 58 deletions.
17 changes: 10 additions & 7 deletions KAPinField/Example/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<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">
<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">
<device id="retina4_0" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17125"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="21505"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="System colors in document resources" minToolsVersion="11.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
Expand Down Expand Up @@ -34,6 +34,9 @@
<color key="tintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="onTintColor" red="0.094117647060000004" green="0.5450980392" blue="0.96078431369999995" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<color key="thumbTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<connections>
<action selector="toggleSecure" destination="BYZ-38-t0r" eventType="valueChanged" id="ptC-yn-G11"/>
</connections>
</switch>
</subviews>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
Expand All @@ -57,15 +60,15 @@
</constraints>
</view>
<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">
<rect key="frame" x="121" y="157" width="78" height="17"/>
<rect key="frame" x="121" y="145" width="78" height="17"/>
<constraints>
<constraint firstAttribute="height" constant="17" id="xM1-qL-uy3"/>
</constraints>
<fontDescription key="fontDescription" type="system" pointSize="14"/>
<nil key="highlightedColor"/>
</label>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="ZrA-JK-GPg" userLabel="embedViewTest">
<rect key="frame" x="0.0" y="194" width="320" height="50"/>
<rect key="frame" x="0.0" y="182" width="320" height="50"/>
<subviews>
<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">
<rect key="frame" x="18" y="0.0" width="284" height="50"/>
Expand All @@ -86,7 +89,7 @@
</view>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="deI-wI-pgE">
<rect key="frame" x="92" y="264" width="136" height="40"/>
<color key="backgroundColor" white="1" alpha="0.14506635273972604" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstAttribute="height" constant="40" id="o02-23-GNN"/>
</constraints>
Expand All @@ -96,14 +99,14 @@
<color key="titleColor" systemColor="systemBlueColor"/>
</state>
<connections>
<action selector="refreshPinField" destination="BYZ-38-t0r" eventType="touchUpInside" id="6Le-gZ-Rl0"/>
<action selector="setupPinfield" destination="BYZ-38-t0r" eventType="touchUpInside" id="q70-6w-eig"/>
</connections>
</button>
</subviews>
<viewLayoutGuide key="safeArea" id="aDb-oH-ANJ"/>
<color key="backgroundColor" systemColor="systemBackgroundColor"/>
<constraints>
<constraint firstItem="deI-wI-pgE" firstAttribute="top" secondItem="ZrA-JK-GPg" secondAttribute="bottom" constant="20" id="7CR-QR-oP5"/>
<constraint firstItem="deI-wI-pgE" firstAttribute="top" secondItem="ZrA-JK-GPg" secondAttribute="bottom" constant="32" id="7CR-QR-oP5"/>
<constraint firstItem="deI-wI-pgE" firstAttribute="centerX" secondItem="8bC-Xf-vdC" secondAttribute="centerX" id="8PS-0h-axA"/>
<constraint firstItem="Ee6-G1-KAi" firstAttribute="centerX" secondItem="aDb-oH-ANJ" secondAttribute="centerX" id="8wt-lz-a2B"/>
<constraint firstItem="aDb-oH-ANJ" firstAttribute="top" secondItem="u2o-qm-AuS" secondAttribute="top" id="9ha-nD-VAo"/>
Expand Down
78 changes: 40 additions & 38 deletions KAPinField/Example/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ class ViewController: UIViewController {
return .lightContent
}



override func viewDidLoad() {
super.viewDidLoad()

// -- Appearance --
self.updateStyle()

// -- Properties --
pinField.properties.delegate = self
self.refreshPinField()
self.setupPinfield()

// Get focus
pinField.becomeFirstResponder()
Expand All @@ -49,50 +43,58 @@ class ViewController: UIViewController {
return string
}

@IBAction func refreshPinField() {
@IBAction func setupPinfield() {

// Random numberOfCharacters
pinField.text = ""
pinField.properties.numberOfCharacters = [4,5].randomElement()!

pinField.updateProperties { properties in
properties.numberOfCharacters = [4,5].randomElement()!
properties.delegate = self
}

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

self.updateStyle()
self.refresh()
}

func updateStyle() {

pinField.properties.isSecure = self.secureSwitch.isOn

@IBAction func toggleSecure() {
self.refresh()
self.pinField.becomeFirstResponder()
}

func refresh() {

self.targetCodeLabel.textColor = UIColor.label.withAlphaComponent(0.8)

pinField.properties.token = "-"
pinField.properties.animateFocus = false
pinField.properties.isUppercased = false
pinField.properties.validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"

// let startIndex = self.targetCode.index(self.targetCode.startIndex, offsetBy: 0)
// let endIndex = self.targetCode.index(self.targetCode.startIndex, offsetBy: 1)
// pinField.text = String(self.targetCode[startIndex...endIndex])
pinField.updateProperties { properties in
properties.isSecure = self.secureSwitch.isOn
properties.token = ""
properties.animateFocus = false
properties.isUppercased = false
properties.validCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
}

pinField.appearance.tokenColor = UIColor.label.withAlphaComponent(0.2)
pinField.appearance.tokenFocusColor = UIColor.label.withAlphaComponent(0.2)
pinField.appearance.textColor = UIColor.label
pinField.appearance.font = .courierBold(40)
pinField.appearance.kerning = 24
pinField.appearance.backOffset = 5
pinField.appearance.backColor = UIColor.clear
pinField.appearance.backBorderWidth = 1
pinField.appearance.backBorderColor = UIColor.label.withAlphaComponent(0.2)
pinField.appearance.backCornerRadius = 4
pinField.appearance.backFocusColor = UIColor.clear
pinField.appearance.backBorderFocusColor = UIColor.label.withAlphaComponent(0.8)
pinField.appearance.backActiveColor = UIColor.clear
pinField.appearance.backBorderActiveColor = UIColor.label
pinField.appearance.backRounded = false
pinField.updateAppearence { appearance in
appearance.tokenColor = UIColor.clear
appearance.tokenFocusColor = UIColor.clear
appearance.textColor = UIColor.label
appearance.font = .menlo(40)
appearance.kerning = 24
appearance.backOffset = 8
appearance.backColor = UIColor.clear
appearance.backBorderWidth = 1
appearance.backBorderColor = UIColor.label.withAlphaComponent(0.2)
appearance.backCornerRadius = 4
appearance.backFocusColor = UIColor.clear
appearance.backBorderFocusColor = UIColor.systemBlue
appearance.backActiveColor = UIColor.clear
appearance.backBorderActiveColor = UIColor.label
appearance.backRounded = false
}
}
}

Expand All @@ -117,7 +119,7 @@ extension ViewController : KAPinFieldDelegate {
} else {
print("Success")
field.animateSuccess(with: "👍") {
self.refreshPinField()
self.setupPinfield()
}

}
Expand Down
20 changes: 7 additions & 13 deletions KAPinField/Sources/KAPinField.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,7 @@ public struct KAPinFieldProperties {
public var token: Character = "" {
didSet {
precondition(!validCharacters.contains(token), "🚫 token can't be one of the valid characters \"\(token)\"")

// Change space to insecable space
if token == " " {
self.token = " "
}
precondition(!token.isWhitespace, "🚫 token can't be a whitespace. Please use a token with a clear color to achieve the same effect")
}
}
public var animateFocus : Bool = true
Expand Down Expand Up @@ -189,7 +185,6 @@ public class KAPinField : UITextField {
}

// back views

var myText = ""
for _ in 0..<self.properties.numberOfCharacters {
myText += "0"
Expand Down Expand Up @@ -419,7 +414,6 @@ public class KAPinField : UITextField {
let duration: Double = isClear ? 0.3 : 0.6
if isClear{
atts[.foregroundColor] = self.appearance.tokenFocusColor
?? self.appearance.tokenColor
} else {
atts[.foregroundColor] = UIColor.clear
}
Expand Down Expand Up @@ -450,9 +444,9 @@ public class KAPinField : UITextField {

self.sanitizeText()


let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center

let font = self.appearance.font?.font() ?? self.font ?? UIFont.preferredFont(forTextStyle: .headline)
self.attributes = [ .paragraphStyle : paragraph,
.font : font,
Expand Down Expand Up @@ -497,8 +491,8 @@ public class KAPinField : UITextField {
backView.layer.borderColor = self.appearance.backBorderColor.cgColor
} else {
attributes[.foregroundColor] = self.appearance.textColor
backView.backgroundColor = self.appearance.backActiveColor ?? self.appearance.backColor
backView.layer.borderColor = self.appearance.backBorderActiveColor.cgColor ?? self.appearance.backBorderColor.cgColor
backView.backgroundColor = self.appearance.backActiveColor
backView.layer.borderColor = self.appearance.backBorderActiveColor.cgColor
}
}

Expand All @@ -507,6 +501,7 @@ public class KAPinField : UITextField {
if i == indexForKernFix {
attributes[.kern] = 0.0
}

attString.append(NSAttributedString(string: string, attributes: attributes))
}

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

// Avoid long fade from tick()
Expand All @@ -604,8 +598,8 @@ public class KAPinField : UITextField {
backIndex = max(backIndex, 0)
if !self.backViews.isEmpty && backIndex < self.backViews.count {
let backView = self.backViews[backIndex]
backView.backgroundColor = self.appearance.backFocusColor ?? self.appearance.backColor
backView.layer.borderColor = self.appearance.backBorderFocusColor.cgColor ?? self.appearance.backBorderColor.cgColor
backView.backgroundColor = self.appearance.backFocusColor
backView.layer.borderColor = self.appearance.backBorderFocusColor.cgColor
}
}
}
Expand Down

0 comments on commit 21f3640

Please sign in to comment.