GithubHelp home page GithubHelp logo

swiftformattedtable's Introduction

SwiftFormattedTable

A formatter for monospace font printing

Background

One of the iOS app I've built is required to build a table formatter which can print receipt for a thermal printer. The printer has a fixed character length, it prints monospace font, and it accepts text only.

This demo is a refactor modification, since the previous code is private.

Example

original

The receipt can be constructed as follow:

let seperator: Character = "="
let row = HeaderRow(cells: [
    Cell(content: "Brandy's General Store\n321 Any Street\nAnytown, NY 10121\n(212) 555-5555", alignment: .center)])
row.addLine(withCells: seperator.fill())
row.addLine(withCells: "Friday, Dec 17, 2013 12:04 PM 14792 Bradley J")
row.addLine(withCells: seperator.fill())

let table = HeaderRow(cells: [Cell(content: "Hot Dog"),
      Cell(content: 2.25.dollar(), alignment: .right, type: .shrinkToFit)])
table.addLine(withCells: "Egg Roll", 2.dollar())
table.addLine(withCells: "Hot Pretzel", 1.75.dollar())
table.addLine(withCells: "Cheese Danish", 2.99.dollar())
table.addLine(withCells: "Jersey, Grey XL", 24.99.dollar())
table.addLine(withCells: seperator.fill(spanCol: 2))
table.addLine(withCells: "Item Count: 5", "Subtotal: \(33.98.dollar())")
table.addLine(withCells: "Sales Tax: \(0.dollar())".span(col: 2, alignment: .right))
table.addLine(withCells: "", seperator.fill())
table.addLine(withCells: "Receipt 22317", "Total: \(33.98.dollar())")
table.addEmptyLine()
table.addLine(withCells: "Cash: \(50.dollar())".span(col: 2, alignment: .right))
table.addLine(withCells: "Cash: \((-16.02).dollar())".span(col: 2, alignment: .right))
table.addEmptyLine()
table.addLine(withCells: seperator.fill(spanCol: 2))
table.addLine(withCells: "Thank you for Shopping at Brandy's!".span(col: 2, alignment: .center))

let master = Table(bound: 48, rows: [row, table])
textView.text = master.print().joined(separator: "\n")

Requirements

  • Minimum iOS version: 11.3
  • XCode 9.4
  • Swift 4.1

Author

Jacky Tay - Software Developer - Smudge

License

SwiftStickyHeaderTableView is available under the MIT license. See the LICENSE file for more info.

swiftformattedtable's People

Contributors

jacky-tay avatar

Watchers

 avatar  avatar  avatar

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.