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:
.package(url: "https://github.com/nicklockwood/SVGPath.git", .upToNextMinor(from: "1.0.0")),
Usage
You can create an instance of SVGPath as follows:
let svgPath = try SVGPath(string: "M150 0 L75 200 L225 200 Z")
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:
let cgPath = try CGPath.from(svgPath: "M150 0 L75 200 L225 200 Z")
Once you have a CGPath
you can render it on iOS or macOS using a CoreGraphics context or a CAShapeLayer
.
Credits
The SVGPath library is primarily the work of Nick Lockwood.