Giter VIP home page Giter VIP logo

formui's Introduction

FormUI

Platform

Powerfully simple form builder for UIKit.


Overview

FormUI provides an easy way to build native forms for iOS. It is inspired by SwiftUI and takes advantage of new technologies like Combine and result builders. However, it is entirely implemented in UIKit which gives it more options for customization.

FormUI aims to be lightweight and unopinionated. It solves the troublesome parts of making formsβ€”like counting sections and hiding rowsβ€”but leaves you to fill in the details for your specific use case.


Examples

New Event Profile Display Settings

Requirements

  • iOS 13.0+
  • Swift 5.3+

Installation

Swift Package Manager

To install FormUI using the Swift Package Manager, add the following value to your Package.swift:

dependencies: [
    .package(url: "https://github.com/james01/FormUI.git", .upToNextMajor(from: "0.1.1"))
]

Usage

Creating a Form

Create a form by subclassing FormViewController.

import FormUI

class MyFormViewController: FormViewController {

    convenience init() {
        self.init(style: .grouped)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        form = Form {
            Section {
                Row(style: .default) { (cell) in
                    cell.textLabel?.text = "Hello World"
                }
            }
        }
    }
}

The Form and Section initializers take advantage of the result builder feature of Swift that was unofficially introduced in Swift 5.1, and officially introduced in Swift 5.4.

Handling Row Selection

Handle row selection by passing a handler to the onSelect(_:) method of a row. This handler will be called every time the row is selected.

Row(style: .default) { (cell) in
    cell.textLabel?.text = "Tap Me"
}.onSelect { (tableView, indexPath) in
    tableView.deselectRow(at: indexPath, animated: true)
    print("Hello World")
}

Dynamically Hiding and Showing Rows

FormUI makes it easy to hide and show rows. This is an otherwise complicated task when using the default UITableView.

First, define a published variable to keep track of whether the row is hidden.

@Published var isRowHidden = true

Then, pass the projected value of that variable (i.e., $isRowHidden) to the hidden(_:) method on the row you want to hide. Every time isRowHidden changes its value, the row will automatically show or hide itself.

For more information on the Published property wrapper, see its documentation.

Section {
    Row(style: .default) { (cell) in
        cell.textLabel?.text = "Tap to Toggle Row"
    }.onSelect { (tableView, indexPath) in
        tableView.deselectRow(at: indexPath, animated: true)
        self.isRowHidden.toggle()
    }

    Row(style: .default) { (cell: UITableViewCell) in
        cell.textLabel?.text = "Hidden"
    }.hidden($isRowHidden)

    Row(style: .default) { (cell) in
        cell.textLabel?.text = "Not Hidden"
    }
}

You can hide and show sections in a similar way.

Using ForEach

Oftentimes you'll want to generate rows or sections from a static data source such as an enum. Use ForEach in these situations.

enum Theme: String, CaseIterable {
    case light
    case dark
    case system
}

...

Section(header: "Theme") {
    ForEach(Theme.self) { (theme) -> Row in
        Row(style: .default) { (cell) in
            cell.textLabel?.text = theme.rawValue.capitalized
        }
    }
}

ForEach can also be used with an array of data.

let desserts = [
    "πŸͺ Cookies",
    "🍫 Chocolate",
    "🍦 Ice Cream",
    "🍭 Candy",
    "🍩 Doughnuts",
    "🍰 Cake"
]

...

Section(header: "Desserts") {
    ForEach(0..<desserts.count) { (n) -> Row in
        Row(style: .default) { (cell) in
            cell.textLabel?.text = desserts[n]
        }
    }
}


Author

James Randolph (@jamesrandolph01)


License

FormUI is released under the MIT license. See LICENSE for details.

formui's People

Contributors

james01 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

eleazertoluan

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.