J.F.B (Jera Form Builder)
JFB 是 Jera 快速创建表单的方式。
我们使用 Eureka 引擎和 Material 组件来设计美观和优秀的表单。
需求
- Xcode 9.0+
- Swift 4.0+
安装
Pod
JFB 通过 CocoaPods 提供使用。在 Podfile 中添加以下行:
pod 'JFB'
Carthage
即将推出
手动
如果您不使用任何依赖管理器,您可以将 JFB 添加到项目中,只添加包含 JFB 类 的文件即可。
同时还需要添加 JFB 的依赖库。
概念
JFB 帮助您使用 Material 组件(即将提供其他布局)创建表单,并包含表单的所有概念如校验、掩码等。
基本上,通过几行代码,您就可以通过操作结果创建表单,例如发送到 API。
FormBuilderViewController
FormBuilderViewController 是构建所有字段并默认创建提交按钮的 UIViewController
。
要创建它,您需要传递 JField 数组,并且它的 FormBuilderDelegate 必须实现以作为 Dictionary 接收表单值。
JField
它是一个所有字段都会实现的协议。
字段列表
- JTextField
- JDateField
- JTimeField
- (即将推出其他)
默认情况下,JField 需要以下属性
- id:这是您从表单中接收值的字典键。
- placeholder:这是字段的占位符。
- validations:设置字段的验证。
用法
如何创建表单
将字段设为数组,并调用FormBuilderViewController
,传递您的字段和代理
import JFB
class ViewController: UIViewController {
let loginFormFields: [JField] = [
JTextField(id: "email", type: .email, placeholder: "E-mail", validations: [.required, .email]),
JTextField(id: "password", type: .password, placeholder: "Password", validations: [.required,
.minLength(6)])
]
lazy var formViewController: FormBuilderViewController = {
return FormBuilderViewController(fields: loginFormFields, delegate: self)
}()
override func viewDidLoad() {
super.viewDidLoad()
formViewController.submitText = .white
formViewController.submitBackgroundColor = .blue
formViewController.submitText = "LOGIN"
}
@IBAction func action(_ sender: Any) {
let nv = UINavigationController(rootViewController: formViewController)
present(nv, animated: true)
}
}
extension ViewController: FormBuilderDelegate {
func formReceivedValues(_ values: [String: Any]) {
print(values)
}
}
自定义
当然,表单的布局也需要配置。因此,我们提供了一些属性来配置表单布局,以满足您的需求。
/// Color to apply at all input tint color and navigation accessory view
open var tintColor: UIColor = Color.blue.base
/// Text to show on submit button
open var submitText: String?
/// Submit button's text color
open var submitTextColor: UIColor
/// Submit button's background color
open var submitBackgroundColor: UIColor
/// Submit button's background color when was disable
open var submitDisableColor: UIColor
/// Submit button's click callback color
open var submitPulseColor: UIColor
如果您需要更多自定义,请让我们知道,我们可以提交一个说明您需要哪种自定义的issue。
验证
每个字段都有自己的验证,因此创建字段时需要传入一个ValidationType
数组。
ValidationType 列表
- cpf
- required
- regex
- maxLength
- minLength
验证实时应用(当用户键入时)并且会显示错误信息。
作者
Vitor Mesquita, [email protected]
许可
JFB受MIT许可。有关更多信息,请参阅LICENSE文件。