Swiftpack.co -  nrivard/BarcodeView as Swift Package
Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
nrivard/BarcodeView
A pure SwiftUI barcode view
.package(url: "https://github.com/nrivard/BarcodeView.git", from: "0.1.4")

BarcodeView

BarcodeView is a beautiful and simple to use SwiftUI barcode view for Apple platforms.

Getting Started

BarcodeView uses the Swift Package Manager. To include it in your project, add BarcodeView as a swift package dependency in XCode and import BarcodeView in any files that need to use it.

Using BarcodeView

Create a barcode

Showing barcodes couldn't be easier with BarcodeView. First, create your Barcode using the BarcodeFactory:

// validate using the checksum digit
guard let barcode = BarcodeFactory.barcode(from: "302993918288") else {
    // invalid barcode
}

// don't worry about whether it's real, you want to display something barcode-like
guard let barcode = BarcodeFactory.barcode(from: "1234567890123", verifyChecksum: false) else {
    // this can only be invalid if you don't have the proper number of digits now
}

Create the view

Now that you have a barcode, create your BarcodeView and give it a height:

var body: some View {
    BarcodeView(barcode)
        .frame(height: 120)
}

"Default barcode"

Customize it

By default, each bar (either clear or foreground color) is 2 points wide. Want to customize it? Change the environment:

var body: some View {
    BarcodeView(barcode)
        .frame(height: 60)
        .environment(\.barWidth, 1)
}

"bar width modified barcode"

Want to change the color? Do it the normal SwiftUI way, using foregroundColor:

var body: some View {
    BarcodeView(barcode)
        .frame(height: 120)
        .foregroundColor(.purple)
}

"purple colored barcode"

If the text doesn't fit your design, get rid of it:

var body: some View {
    BarcodeView(barcode, showText: false)
        .frame(height: 60)
}

"no text barcode"

What's supported

Currently BarcodeView supports the following barcode formats:

  • UPCA (12 digits)
  • EAN13 (13 digits)

In addition, BarcodeView supports light and dark modes, but if you care about actual scannability, don't use dark mode. It looks cool but it's the opposite of what scanners are looking for.

Want to help?

Create an issue and submit a PR against that issue. Areas I would love to see improved are:

  • Add support for additional barcodes, like EAN8 and UPC-E
  • Simplify the cutout and text display code

Have questions?

Create an issue or hit me up on Twitter

GitHub

link
Stars: 8
Last commit: 3 weeks ago

Ad: Job Offers

iOS Software Engineer @ Perry Street Software
Perry Street Software is Jack’d and SCRUFF. We are two of the world’s largest gay, bi, trans and queer social dating apps on iOS and Android. Our brands reach more than 20 million members worldwide so members can connect, meet and express themselves on a platform that prioritizes privacy and security. We invest heavily into SwiftUI and using Swift Packages to modularize the codebase.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API