Lightweight Pin Code Field library for iOS written in Swift

KAPinField

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

Install

With Cocoapods
pod 'KAPinField'

Usage

import KAPinField

class MyController : UIVIewController {
  ...
}

Storyboard

You can add an UITextField directly in your Storyboard scene and declare it as KAPinField. It will automagically become a pin field. You can then customize it from the inspector view to suit your needs.

Delegation

Don't forget to set the delegate likeso :


@IBOutlet var pinField: KAPinField!

override func viewDidLoad() {
        super.viewDidLoad()
        pinField.pinDelegate = self
        ...
}

One simple method will be called on your delegate

extension MyController : KAPinFieldDelegate {
  func pinField(_ field: KAPinField, didFinishWith code: String) {
    print("didFinishWith : \(code)")
  }
}

Properties

pinField.token = "△" // Default to "•"
pinField.numberOfCharacters = 5 // Default to 4
pinField.validCharacters = "0123456789+#?" // Default to only numbers, "0123456789"
pinField.pinText = "123" // You can set part or all of the pin text

Styling

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

Animation

KAPinField also provide some eye-candy for failure and success.

Success
field.animateSuccess(with: "?") {
    print("Success")
}
Failure
field.animateFailure() {
   print("Failure")
}

GitHub