VRCodeView
VRCodeView 是一个轻盈、高度可定制且美观的 UIView 子类,允许用户逐个输入验证码字符,并且可以实现平滑的移动。
示例
要运行示例项目,克隆仓库,然后首先从 Example 目录运行 pod install
兼容性
VRCodeView 兼容 Swift 4.1 与 iOS 10+。
安装
VRCodeView 可通过 CocoaPods 获取。要安装,只需将以下行添加到您的 Podfile 中
pod 'VRCodeView'
使用Storyboard初始化
- 在故事板上拖动一个UIView。
- 使用《强制类型转换》选择Identity Inspector从VRCodeView派生。
- 使用属性检查器进行修改并实时在故事板上查看效果。
使用代码初始化
就像初始化一个普通的UIView一样简单!
var codeView: VRCodeView?
...
override func viewDidLoad() {
super.viewDidLoad()
let width: CGFloat = 200
let codeViewFrame = CGRect(x: view.frame.midX - width/2,
y: 150,
width: width,
height: 65)
codeView = VRCodeView(frame: codeViewFrame)
codeView?.delegate = self
// ... Other setup
}
委托使用
只有一个委托方法
public protocol VRCodeViewDelegate: AnyObject {
func didCompleteCodeEntry(codeEntered: String)
}
当所有字段填写完成后将会调用此方法。它包含了用户输入的代码。注意:为了使用此方法,请确保设置了委托!!
使用
文本字段属性
codeView?.font = UIFont(name: "Avenir", size: 20)!
codeView?.spacing = 5
codeView?.numberOfFields = 3
文本字段边框属性
codeView?.cornerRadius = 10
codeView?.thickBorderColor = UIColor.purple
codeView?.thinBorderColor = UIColor.purple
codeView?.thickBorderWidth = 2.5
codeView?.thinBorderWidth = 1.0
键盘属性
// Type . and see the available options!
codeView?.allowedCharacters = .decimalDigits
codeView?.autocorrectionType = .no
codeView?.spellCheckingType = .no
codeView?.keyboardType = .decimalPad
codeView?.returnKeyType = .go
注意:allowedCharacters属性为CharacterSet类型,因此您可以创建自己的自定义字符集并将其分配给此属性。
阴影工作区
codeView?.backgroundColor = nil
codeView?.layer.shadowColor = UIColor.black.cgColor
codeView?.layer.shadowOffset = CGSize(width: 0, height: 0)
codeView?.layer.shadowRadius = 10.0
codeView?.layer.shadowOpacity = 0.4
其他属性和方法
.code : 返回输入字符的字符串。
@IBAction func showCurrentLetters(_ sender: UIButton) {
print(codeView?.code)
}
isUpperCased : 当设置为 true 时,视图将任何字符转换为相应的大写字母。
codeView?.isUpperCased = true
.hasText : 返回一个布尔值,指示视图是否包含任何字符。
guard codeView?.hasText else {
return
}
// Do something
.clear() : 删除所有输入字符。
@IBAction func clearButtonPressed(_ sender: UIButton) {
codeView?.clear()
}
每个属性的默认值
属性 | 默认值 |
allowedCharacters | CharacterSet.decimalDigits |
keyboardType | .asciiCapable |
returnKeyType | .done |
autocorrectionType | .no |
spellCheckingType | .no |
numberOfFields | 0 |
cornerRadius | 3.0 |
thinBorderWidth | 1.0 |
thickBorderWidth | 3.0 |
thinBorderColor | .black |
thickBorderColor | .black |
spacing | 10 |
font | UIFont.systemFont(ofSize: 16, weight: .semibold) |
isUpperCased | false |
作者
Vatsal Rustagi, [email protected]
许可协议
VRCodeView 在 MIT 许可协议下可用。有关更多信息,请参阅 LICENSE 文件。