Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 15 additions & 15 deletions KPassCode/KPassCode.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
182B39E922CB5CD600D79C5D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 182B39E722CB5CD600D79C5D /* Main.storyboard */; };
182B39EB22CB5CD700D79C5D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 182B39EA22CB5CD700D79C5D /* Assets.xcassets */; };
182B39EE22CB5CD700D79C5D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 182B39EC22CB5CD700D79C5D /* LaunchScreen.storyboard */; };
182B39F822CB5CFC00D79C5D /* PinTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182B39F522CB5CFC00D79C5D /* PinTextField.swift */; };
182B39F922CB5CFC00D79C5D /* PinConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182B39F622CB5CFC00D79C5D /* PinConfig.swift */; };
182B39FA22CB5CFC00D79C5D /* PinView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 182B39F722CB5CFC00D79C5D /* PinView.swift */; };
3B1E119E29540F1B0058D81F /* PinTextField.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B1E119B29540F1B0058D81F /* PinTextField.swift */; };
3B1E119F29540F1B0058D81F /* PinConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B1E119C29540F1B0058D81F /* PinConfig.swift */; };
3B1E11A029540F1B0058D81F /* PinView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B1E119D29540F1B0058D81F /* PinView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand All @@ -25,9 +25,9 @@
182B39EA22CB5CD700D79C5D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
182B39ED22CB5CD700D79C5D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
182B39EF22CB5CD700D79C5D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
182B39F522CB5CFC00D79C5D /* PinTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinTextField.swift; sourceTree = "<group>"; };
182B39F622CB5CFC00D79C5D /* PinConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinConfig.swift; sourceTree = "<group>"; };
182B39F722CB5CFC00D79C5D /* PinView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinView.swift; sourceTree = "<group>"; };
3B1E119B29540F1B0058D81F /* PinTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinTextField.swift; sourceTree = "<group>"; };
3B1E119C29540F1B0058D81F /* PinConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinConfig.swift; sourceTree = "<group>"; };
3B1E119D29540F1B0058D81F /* PinView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -60,7 +60,7 @@
182B39E222CB5CD600D79C5D /* KPassCode */ = {
isa = PBXGroup;
children = (
182B39FB22CB5D1100D79C5D /* KPin */,
3B1E119A29540F1B0058D81F /* Source */,
182B39E322CB5CD600D79C5D /* AppDelegate.swift */,
182B39E522CB5CD600D79C5D /* ViewController.swift */,
182B39E722CB5CD600D79C5D /* Main.storyboard */,
Expand All @@ -71,14 +71,14 @@
path = KPassCode;
sourceTree = "<group>";
};
182B39FB22CB5D1100D79C5D /* KPin */ = {
3B1E119A29540F1B0058D81F /* Source */ = {
isa = PBXGroup;
children = (
182B39F622CB5CFC00D79C5D /* PinConfig.swift */,
182B39F522CB5CFC00D79C5D /* PinTextField.swift */,
182B39F722CB5CFC00D79C5D /* PinView.swift */,
3B1E119B29540F1B0058D81F /* PinTextField.swift */,
3B1E119C29540F1B0058D81F /* PinConfig.swift */,
3B1E119D29540F1B0058D81F /* PinView.swift */,
);
path = KPin;
path = Source;
sourceTree = "<group>";
};
/* End PBXGroup section */
Expand Down Expand Up @@ -152,11 +152,11 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
182B39FA22CB5CFC00D79C5D /* PinView.swift in Sources */,
3B1E11A029540F1B0058D81F /* PinView.swift in Sources */,
182B39E622CB5CD600D79C5D /* ViewController.swift in Sources */,
182B39F822CB5CFC00D79C5D /* PinTextField.swift in Sources */,
3B1E119E29540F1B0058D81F /* PinTextField.swift in Sources */,
182B39E422CB5CD600D79C5D /* AppDelegate.swift in Sources */,
182B39F922CB5CFC00D79C5D /* PinConfig.swift in Sources */,
3B1E119F29540F1B0058D81F /* PinConfig.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ public struct PinConfig {
public var spacing: CGFloat?
public var isSecureTextEntry: Bool?
public var placeHolderText: String?
public var placeHolderAttributedText: NSAttributedString?
public var showPlaceHolder: Bool?
init(numberOfFields:OTPLength = .four, dotColor:UIColor = UIColor.black, lineColor:UIColor = .clear, viewMain:UIView = UIView(), spacing:CGFloat = 8.0, secureTextEntry: Bool = true, placeHolderText: String = "*", showPlaceHolder: Bool = true) {
public init(numberOfFields:OTPLength = .four, dotColor:UIColor = UIColor.black, lineColor:UIColor = .clear, viewMain:UIView = UIView(), spacing:CGFloat = 8.0, secureTextEntry: Bool = true, placeHolderText: String = "*", showPlaceHolder: Bool = true) {
self.otpLength = numberOfFields
self.dotColor = dotColor
self.lineColor = lineColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ public enum OTPLength {
}
}

class PinView: UIStackView, UITextFieldDelegate, OTPTextFieldDelegate {
public class PinView: UIStackView, UITextFieldDelegate, OTPTextFieldDelegate {

lazy public var config:PinConfig! = PinConfig()
var textFields = [UITextField]()
public var textFields = [UITextField]()

// MARK: Initialization

Expand Down Expand Up @@ -83,6 +83,9 @@ class PinView: UIStackView, UITextFieldDelegate, OTPTextFieldDelegate {
textField.delegate = self
textField.isSecureTextEntry = config.isSecureTextEntry!
textField.placeholder = config.showPlaceHolder! ? config.placeHolderText : ""
if let attributedPH = config.placeHolderAttributedText {
textField.attributedPlaceholder = attributedPH
}
textField.backgroundColor = .white
textField.textAlignment = .center
textField.borderStyle = .none
Expand All @@ -106,27 +109,35 @@ class PinView: UIStackView, UITextFieldDelegate, OTPTextFieldDelegate {
}
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.characters.count == 0 {
if textField.text?.characters.count == 0 {
public func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
if string.count == 0 {
if textField.text?.count == 0 {
if let previousTextField = self.viewWithTag(textField.tag-1) {
previousTextField.becomeFirstResponder()
}
}
} else {
} else if string.count == self.config.otpLength?.value {
for (idx,textField) in textFields.enumerated() {
let txt = String(string[string.index(string.startIndex, offsetBy: idx)])
textField.text = txt
}
return false
} else if string.count == 1 {
if let currentTextField = self.viewWithTag(textField.tag+1) {
currentTextField.becomeFirstResponder()
} else {
if !textField.text!.isEmpty {
return false
}
}
} else {
return false
}
textField.text = string
return false
}

func getOTP() throws -> String {
public func getOTP() throws -> String {
var otpCode:String = ""
for textField in textFields {
if textField.text == "" {
Expand Down
6 changes: 5 additions & 1 deletion KPassCode/KPassCode/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ class ViewController: UIViewController {
config.lineColor = #colorLiteral(red: 0.8265652657, green: 0.8502194881, blue: 0.9000532627, alpha: 1)
config.spacing = 20
config.isSecureTextEntry = false
config.showPlaceHolder = false
config.showPlaceHolder = true
config.placeHolderAttributedText = NSAttributedString(string: "-", attributes: [NSAttributedString.Key.foregroundColor:UIColor.black, NSAttributedString.Key.font: UIFont(name: "Avenir-Light", size: 24.0)!])


config.lineColor = .clear

viewOTP.config = config
viewOTP.setUpView()
Expand Down
13 changes: 13 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// swift-tools-version:5.0

import PackageDescription

let package = Package(name: "KPassCode",
platforms: [.iOS(.v11)],
products: [.library(name: "KPassCode",
targets: ["KPassCode"])],
targets: [.target(name: "KPassCode",
path: "KPassCode/KPassCode/Source",
linkerSettings: [])],
swiftLanguageVersions: [.v5])