├── .gitignore ├── CodeInputView.xcodeproj ├── project.pbxproj └── project.xcworkspace │ └── contents.xcworkspacedata ├── CodeInputView ├── CodeInputView.swift └── Info.plist ├── README.md └── Screenshots.gif /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | xcuserdata 3 | xcshareddata 4 | -------------------------------------------------------------------------------- /CodeInputView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | D2D088C81CCDC2BB00FAAE8F /* CodeInputView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D2D088C71CCDC2BB00FAAE8F /* CodeInputView.swift */; }; 11 | /* End PBXBuildFile section */ 12 | 13 | /* Begin PBXFileReference section */ 14 | D2D088AD1CCDC1D700FAAE8F /* CodeInputView.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = CodeInputView.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 15 | D2D088B21CCDC1D700FAAE8F /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 16 | D2D088C71CCDC2BB00FAAE8F /* CodeInputView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CodeInputView.swift; sourceTree = ""; }; 17 | D2D088C91CCDC32D00FAAE8F /* README.md */ = {isa = PBXFileReference; lastKnownFileType = net.daringfireball.markdown; path = README.md; sourceTree = ""; }; 18 | D2D088CA1CCDC36700FAAE8F /* Screenshots.gif */ = {isa = PBXFileReference; lastKnownFileType = image.gif; path = Screenshots.gif; sourceTree = ""; }; 19 | /* End PBXFileReference section */ 20 | 21 | /* Begin PBXFrameworksBuildPhase section */ 22 | D2D088A91CCDC1D700FAAE8F /* Frameworks */ = { 23 | isa = PBXFrameworksBuildPhase; 24 | buildActionMask = 2147483647; 25 | files = ( 26 | ); 27 | runOnlyForDeploymentPostprocessing = 0; 28 | }; 29 | /* End PBXFrameworksBuildPhase section */ 30 | 31 | /* Begin PBXGroup section */ 32 | D2D088A31CCDC1D700FAAE8F = { 33 | isa = PBXGroup; 34 | children = ( 35 | D2D088AF1CCDC1D700FAAE8F /* CodeInputView */, 36 | D2D088AE1CCDC1D700FAAE8F /* Products */, 37 | D2D088C91CCDC32D00FAAE8F /* README.md */, 38 | D2D088CA1CCDC36700FAAE8F /* Screenshots.gif */, 39 | ); 40 | sourceTree = ""; 41 | }; 42 | D2D088AE1CCDC1D700FAAE8F /* Products */ = { 43 | isa = PBXGroup; 44 | children = ( 45 | D2D088AD1CCDC1D700FAAE8F /* CodeInputView.framework */, 46 | ); 47 | name = Products; 48 | sourceTree = ""; 49 | }; 50 | D2D088AF1CCDC1D700FAAE8F /* CodeInputView */ = { 51 | isa = PBXGroup; 52 | children = ( 53 | D2D088C71CCDC2BB00FAAE8F /* CodeInputView.swift */, 54 | D2D088B21CCDC1D700FAAE8F /* Info.plist */, 55 | ); 56 | path = CodeInputView; 57 | sourceTree = ""; 58 | }; 59 | /* End PBXGroup section */ 60 | 61 | /* Begin PBXHeadersBuildPhase section */ 62 | D2D088AA1CCDC1D700FAAE8F /* Headers */ = { 63 | isa = PBXHeadersBuildPhase; 64 | buildActionMask = 2147483647; 65 | files = ( 66 | ); 67 | runOnlyForDeploymentPostprocessing = 0; 68 | }; 69 | /* End PBXHeadersBuildPhase section */ 70 | 71 | /* Begin PBXNativeTarget section */ 72 | D2D088AC1CCDC1D700FAAE8F /* CodeInputView */ = { 73 | isa = PBXNativeTarget; 74 | buildConfigurationList = D2D088C11CCDC1D700FAAE8F /* Build configuration list for PBXNativeTarget "CodeInputView" */; 75 | buildPhases = ( 76 | D2D088A81CCDC1D700FAAE8F /* Sources */, 77 | D2D088A91CCDC1D700FAAE8F /* Frameworks */, 78 | D2D088AA1CCDC1D700FAAE8F /* Headers */, 79 | D2D088AB1CCDC1D700FAAE8F /* Resources */, 80 | ); 81 | buildRules = ( 82 | ); 83 | dependencies = ( 84 | ); 85 | name = CodeInputView; 86 | productName = CodeInputView; 87 | productReference = D2D088AD1CCDC1D700FAAE8F /* CodeInputView.framework */; 88 | productType = "com.apple.product-type.framework"; 89 | }; 90 | /* End PBXNativeTarget section */ 91 | 92 | /* Begin PBXProject section */ 93 | D2D088A41CCDC1D700FAAE8F /* Project object */ = { 94 | isa = PBXProject; 95 | attributes = { 96 | LastSwiftUpdateCheck = 0730; 97 | LastUpgradeCheck = 0930; 98 | ORGANIZATIONNAME = "Acani LLC"; 99 | TargetAttributes = { 100 | D2D088AC1CCDC1D700FAAE8F = { 101 | CreatedOnToolsVersion = 7.3; 102 | LastSwiftMigration = 1020; 103 | }; 104 | }; 105 | }; 106 | buildConfigurationList = D2D088A71CCDC1D700FAAE8F /* Build configuration list for PBXProject "CodeInputView" */; 107 | compatibilityVersion = "Xcode 3.2"; 108 | developmentRegion = en; 109 | hasScannedForEncodings = 0; 110 | knownRegions = ( 111 | en, 112 | Base, 113 | ); 114 | mainGroup = D2D088A31CCDC1D700FAAE8F; 115 | productRefGroup = D2D088AE1CCDC1D700FAAE8F /* Products */; 116 | projectDirPath = ""; 117 | projectRoot = ""; 118 | targets = ( 119 | D2D088AC1CCDC1D700FAAE8F /* CodeInputView */, 120 | ); 121 | }; 122 | /* End PBXProject section */ 123 | 124 | /* Begin PBXResourcesBuildPhase section */ 125 | D2D088AB1CCDC1D700FAAE8F /* Resources */ = { 126 | isa = PBXResourcesBuildPhase; 127 | buildActionMask = 2147483647; 128 | files = ( 129 | ); 130 | runOnlyForDeploymentPostprocessing = 0; 131 | }; 132 | /* End PBXResourcesBuildPhase section */ 133 | 134 | /* Begin PBXSourcesBuildPhase section */ 135 | D2D088A81CCDC1D700FAAE8F /* Sources */ = { 136 | isa = PBXSourcesBuildPhase; 137 | buildActionMask = 2147483647; 138 | files = ( 139 | D2D088C81CCDC2BB00FAAE8F /* CodeInputView.swift in Sources */, 140 | ); 141 | runOnlyForDeploymentPostprocessing = 0; 142 | }; 143 | /* End PBXSourcesBuildPhase section */ 144 | 145 | /* Begin XCBuildConfiguration section */ 146 | D2D088BF1CCDC1D700FAAE8F /* Debug */ = { 147 | isa = XCBuildConfiguration; 148 | buildSettings = { 149 | ALWAYS_SEARCH_USER_PATHS = NO; 150 | CLANG_ANALYZER_NONNULL = YES; 151 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 152 | CLANG_CXX_LIBRARY = "libc++"; 153 | CLANG_ENABLE_MODULES = YES; 154 | CLANG_ENABLE_OBJC_ARC = YES; 155 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 156 | CLANG_WARN_BOOL_CONVERSION = YES; 157 | CLANG_WARN_COMMA = YES; 158 | CLANG_WARN_CONSTANT_CONVERSION = YES; 159 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 160 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 161 | CLANG_WARN_EMPTY_BODY = YES; 162 | CLANG_WARN_ENUM_CONVERSION = YES; 163 | CLANG_WARN_INFINITE_RECURSION = YES; 164 | CLANG_WARN_INT_CONVERSION = YES; 165 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 166 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 167 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 168 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 169 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 170 | CLANG_WARN_STRICT_PROTOTYPES = YES; 171 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 172 | CLANG_WARN_UNREACHABLE_CODE = YES; 173 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 174 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 175 | COPY_PHASE_STRIP = NO; 176 | CURRENT_PROJECT_VERSION = 1; 177 | DEBUG_INFORMATION_FORMAT = dwarf; 178 | ENABLE_STRICT_OBJC_MSGSEND = YES; 179 | ENABLE_TESTABILITY = YES; 180 | GCC_C_LANGUAGE_STANDARD = gnu99; 181 | GCC_DYNAMIC_NO_PIC = NO; 182 | GCC_NO_COMMON_BLOCKS = YES; 183 | GCC_OPTIMIZATION_LEVEL = 0; 184 | GCC_PREPROCESSOR_DEFINITIONS = ( 185 | "DEBUG=1", 186 | "$(inherited)", 187 | ); 188 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 189 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 190 | GCC_WARN_UNDECLARED_SELECTOR = YES; 191 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 192 | GCC_WARN_UNUSED_FUNCTION = YES; 193 | GCC_WARN_UNUSED_VARIABLE = YES; 194 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 195 | MTL_ENABLE_DEBUG_INFO = YES; 196 | ONLY_ACTIVE_ARCH = YES; 197 | SDKROOT = iphoneos; 198 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 199 | TARGETED_DEVICE_FAMILY = "1,2"; 200 | VERSIONING_SYSTEM = "apple-generic"; 201 | VERSION_INFO_PREFIX = ""; 202 | }; 203 | name = Debug; 204 | }; 205 | D2D088C01CCDC1D700FAAE8F /* Release */ = { 206 | isa = XCBuildConfiguration; 207 | buildSettings = { 208 | ALWAYS_SEARCH_USER_PATHS = NO; 209 | CLANG_ANALYZER_NONNULL = YES; 210 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 211 | CLANG_CXX_LIBRARY = "libc++"; 212 | CLANG_ENABLE_MODULES = YES; 213 | CLANG_ENABLE_OBJC_ARC = YES; 214 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 215 | CLANG_WARN_BOOL_CONVERSION = YES; 216 | CLANG_WARN_COMMA = YES; 217 | CLANG_WARN_CONSTANT_CONVERSION = YES; 218 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 219 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 220 | CLANG_WARN_EMPTY_BODY = YES; 221 | CLANG_WARN_ENUM_CONVERSION = YES; 222 | CLANG_WARN_INFINITE_RECURSION = YES; 223 | CLANG_WARN_INT_CONVERSION = YES; 224 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 225 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 226 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 227 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 228 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 229 | CLANG_WARN_STRICT_PROTOTYPES = YES; 230 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 231 | CLANG_WARN_UNREACHABLE_CODE = YES; 232 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 233 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 234 | COPY_PHASE_STRIP = NO; 235 | CURRENT_PROJECT_VERSION = 1; 236 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 237 | ENABLE_NS_ASSERTIONS = NO; 238 | ENABLE_STRICT_OBJC_MSGSEND = YES; 239 | GCC_C_LANGUAGE_STANDARD = gnu99; 240 | GCC_NO_COMMON_BLOCKS = YES; 241 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 242 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 243 | GCC_WARN_UNDECLARED_SELECTOR = YES; 244 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 245 | GCC_WARN_UNUSED_FUNCTION = YES; 246 | GCC_WARN_UNUSED_VARIABLE = YES; 247 | IPHONEOS_DEPLOYMENT_TARGET = 13.0; 248 | MTL_ENABLE_DEBUG_INFO = NO; 249 | SDKROOT = iphoneos; 250 | SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule"; 251 | TARGETED_DEVICE_FAMILY = "1,2"; 252 | VALIDATE_PRODUCT = YES; 253 | VERSIONING_SYSTEM = "apple-generic"; 254 | VERSION_INFO_PREFIX = ""; 255 | }; 256 | name = Release; 257 | }; 258 | D2D088C21CCDC1D700FAAE8F /* Debug */ = { 259 | isa = XCBuildConfiguration; 260 | buildSettings = { 261 | CLANG_ENABLE_MODULES = YES; 262 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; 263 | DEFINES_MODULE = YES; 264 | DYLIB_COMPATIBILITY_VERSION = 1; 265 | DYLIB_CURRENT_VERSION = 1; 266 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 267 | INFOPLIST_FILE = CodeInputView/Info.plist; 268 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 269 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 270 | PRODUCT_BUNDLE_IDENTIFIER = com.acani.CodeInputView; 271 | PRODUCT_NAME = "$(TARGET_NAME)"; 272 | SKIP_INSTALL = YES; 273 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 274 | SWIFT_VERSION = 5.0; 275 | }; 276 | name = Debug; 277 | }; 278 | D2D088C31CCDC1D700FAAE8F /* Release */ = { 279 | isa = XCBuildConfiguration; 280 | buildSettings = { 281 | CLANG_ENABLE_MODULES = YES; 282 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = ""; 283 | DEFINES_MODULE = YES; 284 | DYLIB_COMPATIBILITY_VERSION = 1; 285 | DYLIB_CURRENT_VERSION = 1; 286 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 287 | INFOPLIST_FILE = CodeInputView/Info.plist; 288 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 289 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; 290 | PRODUCT_BUNDLE_IDENTIFIER = com.acani.CodeInputView; 291 | PRODUCT_NAME = "$(TARGET_NAME)"; 292 | SKIP_INSTALL = YES; 293 | SWIFT_VERSION = 5.0; 294 | }; 295 | name = Release; 296 | }; 297 | /* End XCBuildConfiguration section */ 298 | 299 | /* Begin XCConfigurationList section */ 300 | D2D088A71CCDC1D700FAAE8F /* Build configuration list for PBXProject "CodeInputView" */ = { 301 | isa = XCConfigurationList; 302 | buildConfigurations = ( 303 | D2D088BF1CCDC1D700FAAE8F /* Debug */, 304 | D2D088C01CCDC1D700FAAE8F /* Release */, 305 | ); 306 | defaultConfigurationIsVisible = 0; 307 | defaultConfigurationName = Release; 308 | }; 309 | D2D088C11CCDC1D700FAAE8F /* Build configuration list for PBXNativeTarget "CodeInputView" */ = { 310 | isa = XCConfigurationList; 311 | buildConfigurations = ( 312 | D2D088C21CCDC1D700FAAE8F /* Debug */, 313 | D2D088C31CCDC1D700FAAE8F /* Release */, 314 | ); 315 | defaultConfigurationIsVisible = 0; 316 | defaultConfigurationName = Release; 317 | }; 318 | /* End XCConfigurationList section */ 319 | }; 320 | rootObject = D2D088A41CCDC1D700FAAE8F /* Project object */; 321 | } 322 | -------------------------------------------------------------------------------- /CodeInputView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /CodeInputView/CodeInputView.swift: -------------------------------------------------------------------------------- 1 | import UIKit 2 | 3 | open class CodeInputView: UIView, UIKeyInput { 4 | open var delegate: CodeInputViewDelegate? 5 | private var nextTag = 1 6 | 7 | // MARK: - UIResponder 8 | 9 | open override var canBecomeFirstResponder: Bool { true } 10 | 11 | // MARK: - UIView 12 | 13 | public override init(frame: CGRect) { 14 | super.init(frame: frame) 15 | 16 | // Add six digitLabels 17 | var frame = CGRect(x: 15, y: 10, width: 35, height: 40) 18 | for index in 1...6 { 19 | let digitLabel = UILabel(frame: frame) 20 | digitLabel.font = .systemFont(ofSize: 42) 21 | digitLabel.tag = index 22 | digitLabel.text = "–" 23 | digitLabel.textAlignment = .center 24 | addSubview(digitLabel) 25 | frame.origin.x += 35 + 15 26 | } 27 | } 28 | 29 | // MARK: - NSCoding 30 | 31 | required public init?(coder: NSCoder) { fatalError("init(coder:) hasn't been implemented") } 32 | 33 | // MARK: - UIKeyInput 34 | 35 | public var hasText: Bool { nextTag > 1 ? true : false } 36 | 37 | open func insertText(_ text: String) { 38 | if nextTag < 7 { 39 | (viewWithTag(nextTag)! as! UILabel).text = text 40 | nextTag += 1 41 | 42 | if nextTag == 7 { 43 | var code = "" 44 | for index in 1.. 1 { 54 | nextTag -= 1 55 | (viewWithTag(nextTag)! as! UILabel).text = "–" 56 | } 57 | } 58 | 59 | open func clear() { while nextTag > 1 { deleteBackward() } } 60 | 61 | // MARK: - UITextInputTraits 62 | 63 | open var keyboardType: UIKeyboardType { get { .numberPad } set { } } 64 | } 65 | 66 | public protocol CodeInputViewDelegate { 67 | func codeInputView(_ codeInputView: CodeInputView, didFinishWithCode code: String) 68 | } 69 | -------------------------------------------------------------------------------- /CodeInputView/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | FMWK 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleSignature 20 | ???? 21 | CFBundleVersion 22 | $(CURRENT_PROJECT_VERSION) 23 | NSPrincipalClass 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CodeInputView 2 | 3 | 6-Digit Code Input Text Field 4 | 5 | ![Screenshots][1] 6 | 7 | Setup: [How to add a Git repository to your Xcode project][2] 8 | 9 | Usage: 10 | 11 | ```swift 12 | import UIKit 13 | 14 | class EnterCodeViewController: UIViewController, CodeInputViewDelegate { 15 | override func viewDidLoad() { 16 | super.viewDidLoad() 17 | let frame = CGRect(x: (view.frame.width-315)/2, y: 242, width: 315, height: 60) 18 | let codeInputView = CodeInputView(frame: frame) 19 | codeInputView.delegate = self 20 | view.addSubview(codeInputView) 21 | codeInputView.becomeFirstResponder() 22 | } 23 | 24 | func codeInputView(codeInputView: CodeInputView, didFinishWithCode code: String) { 25 | let title = code == "123456" ? "Correct!" : "Wrong!" 26 | let alert = UIAlertController(title: title, message: nil, preferredStyle: .alert) 27 | alert.addAction(UIAlertAction(title: "OK", style: .cancel) { _ in codeInputView.clear() }) 28 | present(alert, animated: true) 29 | } 30 | } 31 | ``` 32 | 33 | Released under the [Unlicense][3]. 34 | 35 | 36 | [1]: Screenshots.gif 37 | [2]: https://github.com/acani/Libraries 38 | [3]: http://unlicense.org 39 | -------------------------------------------------------------------------------- /Screenshots.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/acani/CodeInputView/fc0bfe9a5c4786373cbfe8b201c895185ab434f8/Screenshots.gif --------------------------------------------------------------------------------