iSwiftForm是一个用于构建iOS表单的轻量级框架。它使用Swift编写,简单易用,易于定制!
- 需求
- XCode 10.2.1+,Swift 5.0+
- 特性
- 支持iOS 8.0+,iPhone和iPad设备
- 自动完成输入字段
- 支持蓝牙键盘
![]() |
![]() |
---|
内容
设置
使用Podfile
[待更新]
直接将源文件复制到项目
-
下载仓库。
-
将所有位于 iSwiftForm/iSwiftForm/Sources/ 下的源文件复制到您的项目。
-
现在它已经准备好了。
import iSwiftForm
[Your code]
运行示例应用
您可以在模拟器或设备上运行示例应用
- 下载仓库。
- 打开 iSwiftForm.xcworkspace 文件。
- 单击目标 iSwiftForm 和 Examples,在 通用 - 代码签名 部分配置您的开发团队。
- 单击左上角的方案 Examples,然后点击运行三角按钮。
示例用法
基本用法
- 创建您的表单类并扩展 FormBaseDataHolder
class DataHolder: FormBaseDataHolder {
// Config the form
override func fillFormData() {
//
}
// Form controls callback
override func valueChanged(sectionNumber: Int, rowNumber: Int, value: String, formData: FormData) -> Bool {
//
}
}
- 重写必要的函数
class DataHolder: FormBaseDataHolder {
var rawData: [String: String] = [:]
// Config the form
override func fillFormData() {
// Create a form group
var group = FormDataGroup("Basic Info")
// Add a Input form control
group += FormData(CellType.Input).config(title: "Name", textSuggestions: ["John Snow", "John Green"]).itemKey("name", self)
// Add a Segmented form control
group += FormData(CellType.SegmentedControl).config(title: "Gender", textSuggestions: ["Male", "Female"]).itemKey("gender", self)
// Add a Date Time form control
group += FormData(CellType.DateTime).config(title: "Birth").itemKey("birth", self).options(["mode": "date"])
// Append this form group
self.append(group)
// Create another form group
group = FormDataGroup("More Info")
// Add a Single Select form control
group += FormData(CellType.ExpandTable).config(title: "Mood", textSuggestions: ["😝", "😭", "☹️"]).itemKey("mood", self)
// Add a Multiple Select form control
group += FormData(CellType.ExpandTable).config(title: "Favorite Foods", textSuggestions: ["🍔", "🍜", "🌽"]).options(["type": "multiple"]).itemKey("food", self)
// Append this form group
self.append(group)
// Create the third form group
group = FormDataGroup()
// Add a Big Button form control
group += FormData(CellType.ButtonBig).config(title: "Submit").options(["color": "none"]).itemKey("submit")
// Append this form group
self.append(group)
}
// Form controls callback
override func valueChanged(sectionNumber: Int, rowNumber: Int, value: String, formData: FormData) -> Bool {
// save the latest form value
self.rawData[formData.key] = value
return true;
}
}
- 显示表单 UI
// Your ViewController
class ViewController: UIViewController {
override func viewDidAppear(_ animated: Bool) {
// Create the form data holder, and attach it to the base form viewcontroller
let vc = DataHolder().attach(FormBaseController());
// Show it
self.present(vc, animated: true, completion: nil);
}
}
二维码扫描按钮
class DataHolder: FormBaseDataHolder, QRScannerCallback {
// Config the form
override func fillFormData() {
... some form data here ...
// Add an Input form control
group += FormData(CellType.Input).config(title: "ID Number", value: self.myFormData["idnumber"])
// Add a Button form control
group += FormData(CellType.Button).config(title: "Scan a bar code").itemKey("scan")
}
// Form controls callback
override func valueChanged(sectionNumber: Int, rowNumber: Int, value: String, formData: FormData) -> Bool {
if (formData.key == "scan") {
Utils.scanBarcode(key: "idnumber", callback: self, from: self.page)
}
return true;
}
// QR Scanner Callback
func qrScannerResult(key: String, value: String) {
self.myFormData[key] = value
// Refresh the form
self.requestUpdate()
}
}
完整 API
全局配置
FormConfigs
FormConfigs 包含一些用于全局表单配置的静态属性。
-
tableViewSectionColor: UIColor(默认值:0xF0F0F5)
- 表视图控制器背景颜色
-
selectedColor: UIColor(默认值:0x024172)
- 表单单元格标题颜色(焦点状态)
-
defaultSelectedColor: UIColor(默认值:0xd9d9d9)
- 表单单元格背景颜色(焦点状态)
-
buttonTextColor: UIColor(默认值:0x005aa0)
- 按钮默认文字颜色
-
bigButtonTextColor: UIColor(默认值:0x0C8E11)
- 大按钮默认文字颜色
-
editBackgroundColor: UIColor(默认值:0xd0d0d0)
- 用于可编辑文本,编辑文本的背景颜色
-
editBorderNokColor: UIColor(默认值:0xE25859)
- 用于可编辑文本,如果编辑文本未被验证,则使用的边框颜色
-
fillCellWithOKColor: Bool(默认值:false)
- 验证后的单元格是否应该填充背景颜色
-
okLightGreenColor: UIColor(默认值:0xd1e9d2)
- 验证后的单元格的背景颜色(仅在 fillCellWithOKColor = true 时显示)
-
okLightGreenColorFocused: UIColor(默认值:0xbedfbf)
- 焦点状态下的验证后单元格的背景颜色(仅在 fillCellWithOKColor = true 时显示)
-
headerAlignment: NSTextAlignment(默认值:.center)
- 表单组标题文字对齐方式
-
headerPadding: UIEdgeInsets?(默认值:UIEdgeInsets(top: 18, left: 18, bottom: 0, right: 0))
- 表单组标题文字内边距
FormBaseDataHolder
-
page: FormBaseController
- 表单视图控制器
-
requestUpdate()
- 重新加载表单数据
-
append(_ formDataGroup: FormDataGroup)
- 添加一个新的表单组
-
attach(_ page: FormBaseController) -> FormBaseController
- 将数据持有者附加到视图控制器,返回新的视图控制器
-
fillFormData()
- 覆盖方法。配置表单数据
-
valueChanged(sectionNumber: Int, rowNumber: Int, value: String, formData: FormData) -> Bool
- 覆盖方法。表单控件回调。返回值目前未使用
-
shouldUpdateWhenAppear() -> Bool
- 覆盖方法。当 base 视图控制器的 viewDidAppear() 方法被调用时,是否重新加载表单数据
FormDataGroup
FormDataGroup 是表单控件的一个组,它在全球表单控件上方显示可选的标题文本。
- init()
- init(_ name: String)
- 构造函数,创建一个新的表单数据组(带有标题文本)
- append(_ formData: FormData)
- += (left: inout FormDataGroup, right: FormData)
- 添加新的表单数据
FormData
FormData 是描述表单控件的基本单元。
-
init(_ type: String)
-
init(_ type: CellType)
- 构造函数,创建一个新的表单控件
-
config(title: String, value: String? = nil) -> FormData
- 设置表单控件的标题和值
-
config(title: String, textSuggestions: [String], value: String? = nil) -> FormData
- 将文本建议作为字符串数组设置。 (用于输入自动完成,选择)
-
config(title: String, textSuggestionSource: TextSuggestionSource, value: String? = nil) -> FormData
- 将文本建议作为类设置。 (用于输入自动完成,选择)
-
itemKey(_ key: String, _ rawData: [String: String]? = nil) -> FormData
- 设置表单控件的键。如果 rawData 不为 nil,则 rawData[key] 将用作 值
-
options(_ options: [String: String]) -> FormData
- 设置表单控件的一些选项。请参阅下文 CellType 以获得详细信息
-
enable(_ enable: Bool) -> FormData
- 表单控件是否启用(用于输入、按钮等...)
-
valuePlaceHolder(_ valuePlaceHolder: String) -> FormData
- 设置输入文本的占位符
-
contentOptions(_ contentOptions: [String: Any]) -> FormData
- 设置内容选项。目前未使用
-
inputType(_ type: InputType) -> FormData
- 设置键盘输入类型
-
validator(_ validator: Validator?) -> FormData
- 设置自定义验证器
-
validator(_ type: Validators, _ options: [String: Any]? = nil) -> FormData
- 设置预设验证器,带有一些选项
-
validate() -> Bool
- 表单控件是否通过验证
当调用 options(_ options: [String: String]) 时,这些选项将被传递给表单单元格。每个单元格由不同的选项控制,以下是它们的列表
Input (CellType.Input)
- 选项: ["bold": "true"]
- 输入字段是否使用粗体文本
Text (CellType.Text)
- 选项: ["type": "edit"]
- 表单控件是否可编辑
选择 (CellType.ExpandTable)
-
选项: ["type": "multiple"]
- 表单控件是否支持多选
-
选项: ["allowDeselect": "true"]
- 表单控件是否允许取消选择项
日期选择器 (CellType.DateTime)
-
选项: ["futureDays": "XX"]
- 允许选择的将来天数最大为XX天
-
选项: ["mode": "date | time"]
- 设置日期选择器的模式。如果不存在'mode'选项,默认模式为日期和时间
-
选项: ["oneDayAfter": "baseDay, format yyyy-MM-dd HH:mm"]
- 设置可选择的日期 [baseDay, baseDay + 1天]
按钮 (CellType.Button)
- 选项: ["style": "delete"]
- 显示不同的按钮样式
单选按钮 (CellType.SegmentedControl)
- 选项: ["disabledSegement": "index, type is Int"]
- 禁用某些单选按钮
大按钮 (CellType.ButtonBig)
- 选项: ["color": "red | gray"]
- 显示不同的按钮样式
签名(CellType.Signature)
- 选项:["disableEditIfNotEmpty": "true"]
- 签完后禁用编辑
扩展框架
添加新的表单控件
- 调用 CellType.register(_ newType: String) 注册新的表单控件类型
- 添加FormXXCell.swift和FormXX.xib源文件来描述视图和行为
- 现在您可以使用 FormData.init(_ type: String) 创建自己的表单控件
贡献
PRs受到欢迎。任何想法请通过电子邮件联系我