BarcodeView is a beautiful and simple to use SwiftUI barcode view for Apple platforms.
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.
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
}
Now that you have a barcode, create your BarcodeView
and give it a height:
var body: some View {
BarcodeView(barcode)
.frame(height: 120)
}
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)
}
Want to change the color? Do it the normal SwiftUI way, using foregroundColor
:
var body: some View {
BarcodeView(barcode)
.frame(height: 120)
.foregroundColor(.purple)
}
If the text doesn't fit your design, get rid of it:
var body: some View {
BarcodeView(barcode, showText: false)
.frame(height: 60)
}
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.
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
Create an issue or hit me up on Twitter