AuthTextField
AuthTextField 允许您轻松地为应用程序的认证界面创建和验证动画文本框。
内容
需求
- iOS 12+
- Swift 5+
- Xcode 10+
安装
AuthTextField 通过 CocoaPods 提供。要安装它,只需将以下行添加到您的 Podfile:
pod 'AuthTextField'
用法
快速开始
代码中使用
import UIKit
import AuthTextField
class ViewController: UIViewController {
let nameTextfield = AuthField()
override func viewDidLoad() {
super.viewDidLoad()
nameTextfield.inputType = .name
self.view.addSubview(nameTextfield)
nameTextfield.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
nameTextfield.leadingAnchor.constraint(equalTo: self.view.leadingAnchor,constant: 20),
nameTextfield.trailingAnchor.constraint(equalTo: self.view.trailingAnchor,constant: -20),
nameTextfield.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor, constant: 20),
nameTextfield.heightAnchor.constraint(equalToConstant: 50)
])
}
}
Storyboard 使用
首先在视图中插入 UIView 对象,然后在 Identity Inspector 中更改 Custom Class -> Class 到 AuthField。将 UIView 连接到 ViewController.swift 文件。
import UIKit
import AuthTextField
class ViewController: UIViewController {
@IBOutlet weak var nameTextField: AuthField!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.inputType = .name
}
}
详细使用
验证
你可以检查文本字段是否满足你的要求,并返回true或false结果。如果结果是错误的,触发动画。
import UIKit
import AuthTextField
class ViewController: UIViewController {
@IBOutlet weak var nameTextField: AuthField!
override func viewDidLoad() {
super.viewDidLoad()
nameTextField.inputType = .name
nameTextField.checkField()
}
}
默认属性
public var inputType: InputTypeSelection?
public var isOptional = false
public var animationType: AnimationTypeSelection = .shake
public var noticeBorderColor: UIColor = .red
public var textColor: UIColor = .lightGray
public var inputColor: UIColor = .lightGray
public var labelFontSmall: UIFont = .systemFont(ofSize: 12)
public var labelFontLarge: UIFont = .boldSystemFont(ofSize: 15)
public var minCharacter: Int = 3
public var isDeleteButtonAvaliable = false
删除按钮
为文本字段添加一个删除按钮并设置按钮颜色。
import UIKit
import AuthTextField
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.inputType = .password
passwordTextField.isDeleteButtonAvaliable = false
passwordTextField.deleteButtonColor = .blue
}
}
动画
动画类型有3种。震动、脉冲和闪烁。如果您不需要动画,请选择.animationType = .none。
import UIKit
import AuthTextField
class ViewController: UIViewController {
@IBOutlet weak var firstTextField: AuthField!
@IBOutlet weak var secondTextField: AuthField!
@IBOutlet weak var thirdTextField: AuthField!
override func viewDidLoad() {
super.viewDidLoad()
firstTextField.inputType = .name
secondTextField.inputType = .name
thirdTextField.inputType = .name
//Setting animation Preset animationType = .shake
firstTextField.animationType = .shake
secondTextField.animationType = .pulse
thirdTextField.animationType = .flash
}
@IBAction func validateButtonPressed(_ sender: Any) {
firstTextField.checkField()
secondTextField.checkField()
thirdTextField.checkField()
}
}
密码
import UIKit
import AuthTextField
class ViewController: UIViewController {
@IBOutlet weak var passwordTextField: AuthField!
override func viewDidLoad() {
super.viewDidLoad()
passwordTextField.inputType = .password
}
@IBAction func validateButtonPressed(_ sender: Any) {
passwordTextField.checkField()
}
}
电子邮件验证
如果选择输入类型为电子邮件时您将对文本字段进行验证,它将自动检查电子邮件格式。
class ViewController: UIViewController {
import UIKit
import AuthTextField
class ViewController: UIViewController {
@IBOutlet weak var emailTextField: AuthField!
override func viewDidLoad() {
super.viewDidLoad()
emailTextField.inputType = .email
}
@IBAction func validateButtonPressed(_ sender: Any) {
emailTextField.checkField()
}
}
创建自定义认证字段
import UIKit
import AuthTextField
class ViewController: UIViewController {
@IBOutlet weak var customFirstTextField: AuthField!
@IBOutlet weak var customSecondTextField: AuthField!
override func viewDidLoad() {
super.viewDidLoad()
customFirstTextField.inputType = .custom
customFirstTextField.setCustomAuthField(isOptional: true, animationType: .flash, noticeColor: .red, textColor: .lightGray, textfieldInputColor: .purple, textFieldInputFont: .systemFont(ofSize: 15), titleLabelSmallSizeFont: .systemFont(ofSize: 12), titleLabelLargeSizeFont: .boldSystemFont(ofSize: 15), placeHolderText: "Custom First Optional", requiredMinCharacter: 1)
customSecondTextField.inputType = .custom
customTextField.setCustomAuthField(isOptional: true, animationType: .flash, noticeColor: .blue, textColor: .orange, textfieldInputColor: .orange, textFieldInputFont: .boldSystemFont(ofSize: 15), titleLabelSmallSizeFont: .systemFont(ofSize: 12), titleLabelLargeSizeFont: .boldSystemFont(ofSize: 15), placeHolderText: "Custom", requiredMinCharacter: 5, isDeleteButtonAvaliable: false, deleteButtonColor: .blue)
}
@IBAction func validateButtonPressed(_ sender: Any) {
customFirstTextField.checkField()
customSecondTextField.checkField()
}
}
作者
ferhanakkan, [email protected]
许可证
AuthTextField可在MIT许可证下使用。有关更多信息,请参阅LICENSE文件。