diff --git a/KPassCode/KPassCode.xcodeproj/project.pbxproj b/KPassCode/KPassCode.xcodeproj/project.pbxproj index 5b38376..75d8cdb 100644 --- a/KPassCode/KPassCode.xcodeproj/project.pbxproj +++ b/KPassCode/KPassCode.xcodeproj/project.pbxproj @@ -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 */ @@ -25,9 +25,9 @@ 182B39EA22CB5CD700D79C5D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 182B39ED22CB5CD700D79C5D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 182B39EF22CB5CD700D79C5D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; - 182B39F522CB5CFC00D79C5D /* PinTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinTextField.swift; sourceTree = ""; }; - 182B39F622CB5CFC00D79C5D /* PinConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinConfig.swift; sourceTree = ""; }; - 182B39F722CB5CFC00D79C5D /* PinView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinView.swift; sourceTree = ""; }; + 3B1E119B29540F1B0058D81F /* PinTextField.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinTextField.swift; sourceTree = ""; }; + 3B1E119C29540F1B0058D81F /* PinConfig.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinConfig.swift; sourceTree = ""; }; + 3B1E119D29540F1B0058D81F /* PinView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PinView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -60,7 +60,7 @@ 182B39E222CB5CD600D79C5D /* KPassCode */ = { isa = PBXGroup; children = ( - 182B39FB22CB5D1100D79C5D /* KPin */, + 3B1E119A29540F1B0058D81F /* Source */, 182B39E322CB5CD600D79C5D /* AppDelegate.swift */, 182B39E522CB5CD600D79C5D /* ViewController.swift */, 182B39E722CB5CD600D79C5D /* Main.storyboard */, @@ -71,14 +71,14 @@ path = KPassCode; sourceTree = ""; }; - 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 = ""; }; /* End PBXGroup section */ @@ -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; }; diff --git a/KPassCode/KPassCode/KPin/PinConfig.swift b/KPassCode/KPassCode/Source/PinConfig.swift similarity index 72% rename from KPassCode/KPassCode/KPin/PinConfig.swift rename to KPassCode/KPassCode/Source/PinConfig.swift index fc48a1a..5d7a7ab 100644 --- a/KPassCode/KPassCode/KPin/PinConfig.swift +++ b/KPassCode/KPassCode/Source/PinConfig.swift @@ -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 diff --git a/KPassCode/KPassCode/KPin/PinTextField.swift b/KPassCode/KPassCode/Source/PinTextField.swift similarity index 100% rename from KPassCode/KPassCode/KPin/PinTextField.swift rename to KPassCode/KPassCode/Source/PinTextField.swift diff --git a/KPassCode/KPassCode/KPin/PinView.swift b/KPassCode/KPassCode/Source/PinView.swift similarity index 83% rename from KPassCode/KPassCode/KPin/PinView.swift rename to KPassCode/KPassCode/Source/PinView.swift index 9e68248..36170a6 100644 --- a/KPassCode/KPassCode/KPin/PinView.swift +++ b/KPassCode/KPassCode/Source/PinView.swift @@ -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 @@ -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 @@ -106,14 +109,20 @@ 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 { @@ -121,12 +130,14 @@ class PinView: UIStackView, UITextFieldDelegate, OTPTextFieldDelegate { 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 == "" { diff --git a/KPassCode/KPassCode/ViewController.swift b/KPassCode/KPassCode/ViewController.swift index 9a2d2d9..0191fcd 100644 --- a/KPassCode/KPassCode/ViewController.swift +++ b/KPassCode/KPassCode/ViewController.swift @@ -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() diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..a851f8f --- /dev/null +++ b/Package.swift @@ -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]) +