SwiftDraw is Swift library for parsing and drawing SVG images and includes a command line tool to convert SVGs into SFSymbol, PNG, PDF and Swift source code.
Create a UIImage directly from an SVG within a bundle, Data or file URL:
import SwiftDraw
let image = UIImage(svgNamed: "sample.svg")
macOS
Create an NSImage directly from an SVG within a bundle, Data or file URL:
import SwiftDraw
let image = NSImage(svgNamed: "sample.svg")
Command line tool
The command line tool converts SVGs to other formats: PNG, JPEG, SFSymbol and Swift source code.
copyright (c) 2022 Simon Whitty
usage: swiftdraw <file.svg> [--format png | pdf | jpeg | swift | sfsymbol] [--size wxh] [--scale 1x | 2x | 3x]
<file> svg file to be processed
Options:
--format format to output image: png | pdf | jpeg | swift | sfsymbol
--size size of output image: 100x200
--scale scale of output image: 1x | 2x | 3x
--insets crop inset of output image: top,left,bottom,right
--precision maximum number of decimal places
--output optional path of output file
--hideUnsupportedFilters hide elements with unsupported filters.
Available keys for --format swift:
--api api of generated code: appkit | uikit
Available keys for --format sfymbol:
--insets alignment of regular variant: top,left,bottom,right | auto
--ultralight svg file of ultralight variant
--ultralightInsets alignment of ultralight variant: top,left,bottom,right | auto
--black svg file of black variant
--blackInsets alignment of black variant: top,left,bottom,right | auto
$ swiftdraw simple.svg --format png --scale 3x
$ swiftdraw simple.svg --format pdf
Installation
You can install the swiftdraw command-line tool on macOS using Homebrew. Assuming you already have Homebrew installed, just type:
$ brew install swiftdraw
To update to the latest version once installed:
$ brew upgrade swiftdraw
Alternatively download the latest command line tool here.
SF Symbol
Custom SF Symbols can be created from a single SVG. SwiftDraw expands strokes, winds paths using the non-zero rule and aligns elements generating a symbol that can be imported directly into Xcode.
Introduction
SwiftDraw is Swift library for parsing and drawing SVG images and includes a command line tool to convert SVGs into SFSymbol, PNG, PDF and Swift source code.
Usage
Vector images can be easily loaded and rasterized to
UIImage
orNSImage
:Rasterize to any size:
Crop the image using insets:
Add padding using negative insets:
iOS
Create a
UIImage
directly from an SVG within a bundle,Data
or fileURL
:macOS
Create an
NSImage
directly from an SVG within a bundle,Data
or fileURL
:Command line tool
The command line tool converts SVGs to other formats: PNG, JPEG, SFSymbol and Swift source code.
Installation
You can install the
swiftdraw
command-line tool on macOS using Homebrew. Assuming you already have Homebrew installed, just type:To update to the latest version once installed:
Alternatively download the latest command line tool here.
SF Symbol
Custom SF Symbols can be created from a single SVG. SwiftDraw expands strokes, winds paths using the non-zero rule and aligns elements generating a symbol that can be imported directly into Xcode.
Optional variants
--ultralight
and--black
can also be provided:Alignment
By default, SwiftDraw automatically sizes and aligns the content to the template guides. The auto alignment insets are output by the tool:
Insets can be provided in the form
--insets top,left,bottom,right
specifying aDouble
orauto
for each edge:Variants can also be aligned using
--ultralightInsets
and--blackInsets
.Swift Code Generation
Swift source code can also be generated from an SVG using the tool:
Source code can be generated using www.whileloop.com/swiftdraw.
Credits
SwiftDraw is primarily the work of Simon Whitty.
(Full list of contributors)