Platform Language CocoaPods Compatible Carthage Compatible Build Status Pod License

KAPinField

一个轻量级的 iOS 密码输入框库,使用 Swift 编写

开箱即用,支持一次性密码自动填充!

示例

安装

使用 Cocoapods pod 'KAPinField'

使用方法

import KAPinField

class MyController : UIVIewController {
  ...
}

Storyboard

您可以直接在 Storyboard 场景中添加一个 UITextField,并将其声明为 KAPinField。 它将自动变成一个密码输入框。 然后,您可以从检查器视图自定义它,以满足您的需求。

代理 (Delegation)

不要忘记像这样设置代理:

@IBOutlet var pinField: KAPinField!

override func viewDidLoad() {
        super.viewDidLoad()
        properties.delegate = self
        ...
}

您的代理将会调用一个简单的方法

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

属性

所有逻辑属性都可以在名为 propertiesKAPinFieldProperties 结构体中找到。

由于 Apple 对尾随空格的处理方式,Token 不能是空格。 您可以使用任何 token,并将 tokenColortokenFocusColor 设置为 .clear 来实现相同的效果。

逻辑
pinField.updateProperties { properties in
  properties.token = "-" // Default to "•", can't be a whitespace !
  properties.numberOfCharacters = 5 // Default to 4
  properties.validCharacters = "0123456789+#?" // Default to only numbers, "0123456789"
  properties.text = "123" // You can set part or all of the text
  properties.animateFocus = true // Animate the currently focused token
  properties.isSecure = false // Secure pinField will hide actual input
  properties.secureToken = "*" // Token used to hide actual character input when using isSecure = true
  properties.isUppercased = false // You can set this to convert input to uppercased.
}
样式

所有的样式都可以通过名为 appearanceKAPinFieldAppearance 结构体来完成。

pinField.updateAppearence { appearance in
  appearance.font = .menloBold(40) // Default to appearance.MonospacedFont.menlo(40)
  appearance.kerning = 20 // Space between characters, default to 16
  appearance.textColor = UIColor.white.withAlphaComponent(1.0) // Default to nib color or black if initialized programmatically.
  appearance.tokenColor = UIColor.black.withAlphaComponent(0.3) // token color, default to text color
  appearance.tokenFocusColor = UIColor.black.withAlphaComponent(0.3)  // token focus color, default to token color
  appearance.backOffset = 8 // Backviews spacing between each other
  appearance.backColor = UIColor.clear
  appearance.backBorderWidth = 1
  appearance.backBorderColor = UIColor.white.withAlphaComponent(0.2)
  appearance.backCornerRadius = 4
  appearance.backFocusColor = UIColor.clear
  appearance.backBorderFocusColor = UIColor.white.withAlphaComponent(0.8)
  appearance.backActiveColor = UIColor.clear
  appearance.backBorderActiveColor = UIColor.white
  appearance.keyboardType = UIKeyboardType.numberPad // Specify keyboard type
}

字体

强烈建议使用等宽字体,以避免输入期间的水平偏移。 为此,提供了一个方便的助手,允许您访问原生 iOS 等宽字体。 要使用它,只需使用 appearance.MonospacedFont 中的枚举值设置 appearance.font。 当然,您仍然可以通过在 KAPinField 上设置默认的 font 属性来使用您自己的字体。

动画

KAPinField 还为失败和成功提供了一些炫酷的视觉效果。

成功
pinfield.animateSuccess(with: "👍") {
    print("Success")
}
失败
pinfield.animateFailure() {
   print("Failure")
}