CocoaPods trunk is moving to be read-only. Read more on the blog, there are 19 months to go.
TestsTested | ✗ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Jan 2018 |
SwiftSwift Version | 4.0 |
SPMSupports SPM | ✗ |
Maintained by Dino Constantinou.
FormKit is API for easily building UITableView based forms in iOS. You create models that represent your form and FormKit takes care of the rest.
import FormKit
class LoginViewController: FormViewController {
// MARK: - UIViewController
override func viewDidLoad() {
super.viewDidLoad()
let section = FormSection()
section.appendFormRow(self.email)
section.appendFormRow(self.password)
let form = Form()
form.appendFormSection(section)
self.form = form
}
// MARK: - Properties
lazy private var email: FormTextField = {
let email = FormTextField()
email.title = "Email"
email.configureCell = { [unowned self] (cell) in
cell.textField.keyboardType = .EmailAddress
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .None
}
return email
}()
lazy private var password: FormTextField = {
let password = FormTextField()
password.title = "Password"
password.configureCell = { [unowned self] (cell) in
cell.textField.secureTextEntry = true
cell.textField.autocorrectionType = .No
cell.textField.autocapitalizationType = .None
}
return password
}()
}
This example demonstrates how you would go about creating a login form using FormKit.
As you can see the easiest way to get started building forms is to subclass FormViewController. Whilst this isn’t strictly necessary it is highly recommended.
The second thing you will notice is that all forms in FormKit are built around the Form model. Form models represent each form and are composed of FormSection models. FormSection consist of FormRowType models and optionally a FormSectionHeaderFooterType for the header and/or footer.
Once you have configured your Form model all you need to do is assign it to the form property of the FormViewController and the rest is taken care for you.
FormKit comes with several FormRowType models that can be used out of the box to create most forms found within iOS.
A type of row that can be used for any cell that doesn’t require user input. This row type would be ideal for creating menus, rows that do not require input or even rows that require completely custom input implementations.
let row = FormRow()
row.icon = UIImage(named: "icon_invitees")
row.title = "Invitees"
row.configureCell = { [unowned self] (cell) in
cell.accessoryType = .DisclosureIndicator
}
row.tap = { [unowned self] (cell) in
self.presentInviteesViewController()
}
A row for accepting input via a UITextField. The UITextField associated with the cell be easily configured using the provided configureCell closure.
let row = FormTextField()
row.title = "URL"
row.text = self.event.URL?.absoluteString
row.configureCell = { (cell) in
cell.textField.keyboardType = .URL
cell.textField.autocapitalizationType = .None
cell.textField.autocorrectionType = .No
}
row.valueDidChange = { [unowned self] (text) in
guard let text = text else {
self.event.URL = nil
return
}
self.event.URL = NSURL(string: text)
}
A row for accepting input via a UITextView. The UITextView associated with the cell be easily configured using the provided configureCell closure.
let row = FormTextView()
row.text = self.event.notes
row.configureCell = { (cell) in
cell.textView.editable = false
cell.textView.selectable = true
cell.textView.dataDetectorTypes = .Link
}
row.valueDidChange = { (text) in
self.event.notes = text
}
A row for accepting input via a UISwitch.
let row = FormSwitch()
row.title = "All Day"
row.on = self.event.day
row.valueDidChange = { [unowned self] (on) in
self.event.day = on
self.configureDateFormatters()
}
A row that allows single or multiple selection of any type.
let row = FormOptionList<Event.Repeat>()
row.title = "Repeat"
row.options = Event.Repeat.all()
row.selection = self.event.repeats
row.stringRepresentatonForOption = { $0.rawValue }
A row for accepting Date and/or Time input from a FormDateTimePicker. This row will insert and remove a FormDateTimePicker beneath the row in the same way that you find in the Event form within the Calendar app.
let row = FormDateTime(formatter: self.dateTimeFormatter)
row.title = "Start"
row.date = self.event.start
row.valueDidChange = { [unowned self] (date) in
self.event.start = date
}
A row for accepting Date and/or Time input from a UIDatePicker. This will show a row with a standalone UIDatePicker.
let row = FormDateTimePicker()
row.valueDidChange = { [unowned self] (date) in
self.date = date
}
A row for showing a button
let row = FormButton()
row.title = "Clear History and Website Data"
row.tap = { [unowned self] (cell) in
self.clear()
}
There is currently one type of FormSectionHeaderFooterType that can be used to show section header or footer text similar to how it would appear in the Settings app.
let section = FormSection()
section.header = FormSectionHeaderFooter(title: "DEMOS")
Dino Constantinou, [email protected]
FormKit is available under the MIT license. See the LICENSE file for more info.