KAPinField
为iOS编写的轻量级密码输入库。此库与全新的iOS 12一次性密码自动填充也兼容良好。
![]() |
示例 |
安装
使用Cocoapods:pod 'KAPinField'
使用
import KAPinField
class MyController : UIVIewController {
...
}
Storyboard
您可以直接在Storyboard场景中添加UITextField,并将其声明为《KAPinField》。它将自动变成密码输入框。然后您可以从检查器视图中进行自定义以满足您的需求。
代理
别忘了设置代理如下
@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)")
}
}
属性
所有逻辑属性都可用在名为 properties
的 KAPinFieldProperties
结构体中。
** 由于苹果处理尾随空格的方式,令牌不能为空白。您可以使用设置为 .clear
的 tokenColor
和 tokenFocusColor
的任何令牌来达到相同的效果 **
逻辑
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.
}
样式
所有样式都可以通过名为 appearance
的 KAPinFieldAppearance
结构体来完成。
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.font
设置为来自 appearance.MonospacedFont
的枚举值。当然,您仍然可以通过在 KAPinField
上设置默认的 font
属性来使用自己的字体。
动画
KAPinField
还提供了一些关于成功和失败的视觉效果。
成功
pinfield.animateSuccess(with: "👍") {
print("Success")
}
故障
pinfield.animateFailure() {
print("Failure")
}