Lightweight Pin Code Field library for iOS, written in Swift. This library also plays well with the all new iOS 12 one time password autofill.
Basic use | iOS12 autofill |
Using Cocoapods
pod 'KAPinField'
One simple delegate method called when the pin code is filled
protocol KAPinFieldDelegate {
func pinfField(_ field: KAPinField, didFinishWith code: String)
}
pinField.token = "◉" // Default to "•"
pinField.numberOfCharacters = 5 // Default to 4
pinField.validCharacters = "0123456789+#?" // Default to "0123456789"
pinField.pinText = "123" // You can set part or all of the pin text
You can use the native defaultTextAttributes
to style KAPinField
.
It's highly recommended to use one of iOS monospaced fonts to avoid weird text offset while editting the field.
let paragraph = NSMutableParagraphStyle()
paragraph.alignment = .center
let attributes : [NSAttributedString.Key : Any] = [
.paragraphStyle : paragraph,
.font : UIFont(name: "Menlo-Regular", size: 40)!,
.kern : 14,
.foregroundColor : UIColor.white]
pinField.defaultTextAttributes = attributes
KAPinField
also provide some eye-candy for failure and success.
field.animateSuccess(with: "👍") {
print("Success")
}
field.animateFailure() {
print("Failure")
}