CBPinEntryView is a view written in Swift to allow easy and slick entry of pins, codes or passwords. It allows backspacing, dismissal of keyboard and continuing where you left off, the whole code is given as a single String or Int and the view is very easily customisable in code or the storyboard.
|Enter pins easily!||Communicate user error||Clear the text field to retry||Secure entry mode|
To run the example project, clone the repo, and run
pod install from the Example directory first.
CBPinEntryView is available through CocoaPods. To install it, simply add the following line to your Podfile:
Put a view into your storyboard or xib and set it's class to
CBPinEntryView. Create an outlet in your file and customise either with the IBInspectable properties or in your code.
Get the code
Get the code with either
There are two delegate methods
entryChanged(_ completed: Bool) and
entryCompleted(with entry: String?). The first will let you know each time the entry is changed, and whether they have completed the entry. The second function will get called when the user fills out the entry to completion and the entered pin will be passed.
Secure entry with customisable secure character (change from ● to ✱ or any other character). Enable
Change the secure entry character by setting the
There is an error mode which can be enabled with
pinEntryView.setError(isError: true) and disabled with
pinEntryView.setError(isError: false). Whether error mode is enabled can be checked with
pinEntryView.resignFirstResponder() will hide the keyboard and disable the error mode.
Allow or restrict characters
allowedEntryTypes and choose between
letters to enable or restrict characters. Set
textFieldCapitalization to choose what kind of capitalisation you would like on the text field.
Customise keyboard type!
The keyboard types are an enum with int raw values. Options are as follows:
0: default // Default type for the current input method. 1: asciiCapable // Displays a keyboard which can enter ASCII characters 2: numbersAndPunctuation // Numbers and assorted punctuation. 3: URL // A type optimized for URL entry (shows . / .com prominently). 4: numberPad // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry. 5: phonePad // A phone pad (1-9, *, 0, #, with letters under the numbers). 6: namePhonePad // A type optimized for entering a person's name or phone number. 7: emailAddress // A type optimized for multiple email address entry (shows space @ . prominently). 8: decimalPad // A number pad with a decimal point. 9: twitter // A type optimized for twitter text entry (easy access to @ #)
CBPinEntryView is available under the MIT license. See the LICENSE file for more info.
You may find interesting
Swift 5, Underline mode + programmatic changes -
Ive merged the PR that allows underline modes and improved it a bit. You can also change the styles on the fly, and it's all updated for Swift 5.
Add alphanumeric support -
Add support for any character, and allow restriction. Also allow text field capitalisation types.
One time code support -
This release adds support for iOS one time codes, and allows setting a background colour on the currently editing field, similar to how you can set the border colour on the currently editing field.
I also fixed a bug where the error mode would clear to the editing border colour rather than the default border colour.
Clear entry! -
Now you can clear the entry with a single function call! I also changed how errors work... toggling wasn't necessarily great. Also this should totally have been 1.6.0 as it has breaking changes... but what are you gonna do, sue me?...
Please don't sue me.
Add spacing as a property -
Allow keyboard type selection -
You can now select the keyboard type! Sorry I didn't think of this before!
Secure entry, become first responder and potential memory leak fix -
I've added secure entry! Want a hidden pin code entry? Just enable
isSecure and choose your secure entry character (or emoji etc!). Get the pin the same way as before.
Swift 4.0 -
Updated code and project settings for Swift 4.0 and Xcode 9
Change Delegate -
At request, I've added a delegate that lets you know when the text field changes so you can auto submit should you wish.
Default border -
If you don't want your cell border to be white or empty by default, set the entryDefaultBorderColour to whatever colour you want!
Error mode! -
I've added an error mode that you can toggle on and off. It is automatically removed when the user taps on a field or starts typing again.
I've also made the border width customisable if you want thicker borders on your entry view!
Made the view IBDesignable and fixed a crash when fetching the pin as an Int.