SVGPath is an open-source parser for the SVG path syntax, making it easy to create CGPaths from this popular format.
SVGPath runs on all Apple platforms, and also Linux (although Linux does not support the CoreGraphics API, so if you need to draw the path you will need to provide your own implementation).
Installation
SVGPath is packaged as a dynamic framework that you can import into your Xcode project. You can install this manually, or by using Swift Package Manager.
Note: SVGPath requires Xcode 10+ to build, and runs on iOS 10+ or macOS 10.12+.
To install using Swift Package Manage, add this to the dependencies: section in your Package.swift file:
Notice that the SVGPath constructor is a throwing function. It will throw an SVGError if the supplied string is invalid or malformed .
Once you have created an SVGPath object, in most cases you’ll want to convert this to a CGPath for rendering on Apple platforms. To do that you can use:
let cgPath = CGPath.from(svgPath: svgPath)
As a shortcut, you can create the CGPath directly from an SVG path string:
Introduction
SVGPath is an open-source parser for the SVG path syntax, making it easy to create
CGPaths
from this popular format.SVGPath runs on all Apple platforms, and also Linux (although Linux does not support the CoreGraphics API, so if you need to draw the path you will need to provide your own implementation).
Installation
SVGPath is packaged as a dynamic framework that you can import into your Xcode project. You can install this manually, or by using Swift Package Manager.
Note: SVGPath requires Xcode 10+ to build, and runs on iOS 10+ or macOS 10.12+.
To install using Swift Package Manage, add this to the
dependencies:
section in your Package.swift file:Usage
You can create an instance of SVGPath as follows:
Notice that the SVGPath constructor is a throwing function. It will throw an
SVGError
if the supplied string is invalid or malformed .Once you have created an
SVGPath
object, in most cases you’ll want to convert this to aCGPath
for rendering on Apple platforms. To do that you can use:As a shortcut, you can create the
CGPath
directly from an SVG path string:Once you have a
CGPath
you can render it on iOS or macOS using a CoreGraphics context or aCAShapeLayer
.Advanced Usage
You can convert a
CGPath
to anSVGPath
using theinit(cgPath:)
initializer:To convert an
SVGPath
back into aString
, use thestring(with:)
method. This can be useful for exporting aCGPath
as an SVG string:It’s also possible to use
SVGPath
without CoreGraphics. You can iterate over the raw path components using thecommands
property:Alternatively, you can use the
points()
orgetPoints()
methods to convert the entire path to a flar array of points at your preferred level of detail.These can be used to render the path using simple straight line segments using any graphics API you choose:
NOTE: coordinates are stored with inverted Y coordinates internally, to match the coordinate system used by Core Graphics on macOS/iOS.
Credits
The SVGPath library is primarily the work of Nick Lockwood.
(Full list of contributors)