今天了解了一下SwiftForms,其实原理跟OC的XLForm是差不多的,可能在用法上有稍微的不同,
-
email
let form = FormDescriptor(title: "Form Title") let section1 = FormSectionDescriptor(headerTitle: "", footerTitle: "") var row = FormRowDescriptor(tag: Static.NameTag, type: .email, title: "Email") row.configuration.cell.appearance = ["textField.placeholder": "wang@gmail.com" as AnyObject, "textField.textAlignment": NSTextAlignment.right.rawValue as AnyObject] section1.rows.append(row)
row.configuration.cell.appearance可以通过这个属性传递一个数组改变cell的某些特征,对齐方式,文字大小,文字颜色等;
-
password 密码输入框
row = FormRowDescriptor(tag: Static.Passwordtag, type: .password, title: "Password") row.configuration.cell.appearance = ["textField.placeholder": "password" as AnyObject, "textField.textAlignment": NSTextAlignment.right.rawValue as AnyObject] section1.rows.append(row)
-
name
row = FormRowDescriptor(tag: Static.FirstTag, type: .name, title: "First Name") row.configuration.cell.appearance = ["textField.placeholder": "First" as AnyObject, "textField.textAlignment": NSTextAlignment.right.rawValue as AnyObject] section2.rows.append(row)
-
text
row = FormRowDescriptor(tag: Static.JobTag, type: .text, title: "Job") row.configuration.cell.appearance = ["textField.placeholder": "Job" as AnyObject, "textField.textAlignment": NSTextAlignment.right.rawValue as AnyObject] section2.rows.append(row)
-
url
row = FormRowDescriptor(tag: Static.UrlTag, type: .url, title: "URL") row.configuration.cell.appearance = ["textField.placeholder": "URL" as AnyObject, "textField.textAlignment": NSTextAlignment.right.rawValue as AnyObject] section3.rows.append(row)
-
phone
row = FormRowDescriptor(tag: Static.PhoneTag, type: .phone, title: "Phone") row.configuration.cell.appearance = ["textField.placeholder": "Phone" as AnyObject, "textField.textAlignment": NSTextAlignment.right.rawValue as AnyObject] section3.rows.append(row)
-
booleanSwitch
row = FormRowDescriptor(tag: Static.SwitchTag, type: .booleanSwitch, title: "Enable") section4.rows.append(row)
-
booleanCheck
row = FormRowDescriptor(tag: Static.CheckTag, type: .booleanCheck, title: "Check")
-
segmentedControl
row = FormRowDescriptor(tag: Static.SegmentTag, type: .segmentedControl, title: "SegmentControl") row.configuration.selection.options = [1, 2, 3, 4] as [AnyObject] row.configuration.selection.optionTitleClosure = { value in guard let option = value as? Int else { return "" } switch option { case 1: return "None" case 2: return "!" case 3: return "!!" case 4 : return "!!!" default: return "" } } row.configuration.cell.appearance = ["titleLabel.font": UIFont.systemFont(ofSize: 20), "segmentedControl.tintColor": UIColor.orange] section4.rows.append(row)
-
picker
let section5 = FormSectionDescriptor(headerTitle: nil, footerTitle: nil) row = FormRowDescriptor(tag: Static.PickerTag, type: .picker, title: "Picker") row.configuration.cell.showsInputToolbar = true row.configuration.selection.options = ["F", "M", "Other"] as [AnyObject] row.configuration.selection.optionTitleClosure = { value in guard let option = value as? String else { return "" } switch option { case "F": return "Female" case "M": return "Male" case "Other": return "Other" default: return "" } } row.value = "M" as AnyObject section5.rows.append(row)
row.configuration.cell.showsInputToolbar = true 显示完成
row.value = "M" as AnyObject 给一个默认的初始值 -
multipleSelector 多选
row = FormRowDescriptor(tag: Static.SelectorTag, type: .multipleSelector, title: "Selector") row.configuration.selection.allowsMultipleSelection = true row.configuration.selection.options = [0, 1, 2, 3, 4] as [AnyObject] row.configuration.selection.optionTitleClosure = { value in guard let option = value as? Int else { return "" } switch option { case 0: return "Aaaa" case 1: return "Bbbb" case 2: return "Cccc" case 3: return "Dddd" case 4: return "Ffff" default: return "" } } row.value = [0, 1] as AnyObject section5.rows.append(row)
-
stepper
let section6 = FormSectionDescriptor(headerTitle: nil, footerTitle: nil) row = FormRowDescriptor(tag: Static.StepperTag, type: .stepper, title: "Stepper") row.configuration.stepper.maximumValue = 200.0 row.configuration.stepper.minimumValue = 20.0 row.configuration.stepper.steps = 20 section6.rows.append(row)
-
slider
row = FormRowDescriptor(tag: Static.SliderTag, type: .slider, title: "Slider") row.configuration.stepper.maximumValue = 100.0 row.configuration.stepper.minimumValue = 0.0 row.configuration.stepper.steps = 10 row.value = 50 as AnyObject section6.rows.append(row)
-
multilineText 多行文本框
let section7 = FormSectionDescriptor(headerTitle: "TEXTVIEW", footerTitle: nil) row = FormRowDescriptor(tag: Static.TextViewTag, type: .multilineText, title: "NOTE") section7.rows.append(row)
-
button
let section8 = FormSectionDescriptor(headerTitle: nil, footerTitle: nil) row = FormRowDescriptor(tag: Static.ButtonTag, type: .button, title: "End Editing") row.configuration.button.didSelectClosure = { _ in self.view.endEditing(true) } section8.rows.append(row)
最后:
form.sections = [section1, section2, section3, section4, section5, section6, section7, section8] self.form = form
这是最主要使用的几种,如果不能满足,可自定义;