Latest 1.3.1
Homepage https://github.com/mchoe/SwiftSVG
License MIT
Platforms osx 10.10, ios 8.0
Authors

SwiftSVG Logo

A simple single pass SVG parser written in Swift.

SwiftSVG is part of a larger library I created called Breakfast. I created a separate project because I would like to support as much as the SVG specification as possible. As such, I wanted a place to track issues related specifically to that goal and any changes here will eventually be integrated into Breakfast.

I also wanted to make it possible to use SwiftSVG without installing the full Breakfast Framework, so feel free to use only SwiftSVG in your next project. It was meant to be as lightweight and modular as possible.

Features

  • Multiple interface options (String, UIBezierPath, CAShapeLayer, UIView, and IBDesignable Interface Builder subclass)
  • Strives to be performant. Takes only one pass through path string. 
  • Low memory usage

Table of Contents

Install

SwiftSVG is meant to be dropped in to any project. Pick and choose the files you need, but you can install all the extensions via Carthage:

github "mchoe/SwiftSVG" ~> 1.0

or Cocoapods:

pod 'SwiftSVG'

Usage

SwiftSVG provides multiple interfaces to parse single path strings and SVG files using NSXMLParser.

String+SVG

The simplest way is to parse raw path data using parseSVGPath()

let examplePathData: String = "M150 0 L75 200 L225 200 Z"
let parsedPath: UIBezierPath = parseSVGPath(examplePathData)

You can optionally provide a UIBezierPath for the second argument if you want to fill in an existing path instead of having one returned, say if you want to append to an existing path:

let pathToAppendTo = UIBezierPath()
let parsedPath: UIBezierPath = parseSVGPath(examplePathData, forPath: pathToAppendTo)

Or, you can use the String extension pathFromSVGString():

let parsedPath: UIBezierPath = examplePathData.pathFromSVGString()
Output:

This is not a triangle


UIBezierPath+SVG

SwiftSVG also provides a convenience initializer for UIBezierPath that allows you to either create a UIBezierPath from a path string or from an SVG file:

let shapeLayer = CAShapeLayer()

// UIBezierPath Convenience Initializer
let fistBump = "M63,27c0.4,0.4,0.7,0.8,1.1,1.2c0.4,0.4,0.4,0.9,0.4,1.5c0,1.9-1,3.1-2.3,4.3c-0.2,0.2-0.4,0.3-0.6,0.5c0,0,0,0,0,0.1 c0.3,0.3,0.7,0.6,0.9,1c0.3,0.9,0.3,1.8,0,2.7c-0.4,1.3-1.4,1.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4-1.2C59.6,10.8,58.5,11.1,57.3,11.3z M26.8,58.2 c-0.1,0-0.1,0-0.2,0c0,0.2,0,0.4,0.1,0.5c0.7,1.9,1.4,3.8,2.1,5.6c0.1,0.2,0.3,0.5,0.5,0.7c0.7,0.7,1.3,0.5,1.6-0.6 c-1.1,0-1.9-0.5-2.4-1.5c-0.3-0.5-0.5-1.1-0.7-1.6C27.5,60.2,27.1,59.2,26.8,58.2z M59.1,14.1c-0.2,0.3-0.4,0.5-0.5,0.7 c-0.1,0.2-0.2,0.4-0.1,0.6c0.6,1,2.1,1.3,3.2,0.6C60.8,15.3,59.9,14.7,59.1,14.1z M22.8,37.3c0,0,0.1,0.1,0.1,0.1 c0.4-0.3,0.8-0.5,1.1-0.8c0.1-0.1,0.2-0.4,0.1-0.5c-0.1-0.2-0.2-0.4-0.4-0.4c-0.1,0-0.3,0.2-0.4,0.3C23.2,36.4,23,36.9,22.8,37.3z M50.6,19.8c0.5-0.1,0.8-0.6,0.7-1.3C50.7,18.8,50.3,19.1,50.6,19.8z M52.6,18.7c-0.1,0-0.1,0-0.2,0.1c-0.1,0.3-0.2,0.5-0.2,0.8 c0.1,0.1,0.1,0.1,0.2,0.2c0.2-0.2,0.5-0.3,0.6-0.5C53,19.2,52.8,18.9,52.6,18.7z"
let parsedPath = UIBezierPath(pathString: fistBump)
shapeLayer.path = parsedPath

// Path from SVG File
let svgURL = NSBundle.mainBundle().URLForResource("fistBump", withExtension: "svg")
let pathFromSVGFile = UIBezierPath.pathWithSVGURL(svgURL)
shapelayer.path = pathFromSVGFile
Output:

Put it here!


CAShapeLayer+SVG

You can create a CAShapeLayer from a path string or SVG file:

// From Path String
let pizza = "M185.158,60.775l-0.698-4.896c-0.675-3.783-1.681-7.316-3.208-10.128c-5.67-10.472-48.894-65.225-61.319-68.777c-2.081-0.595-7.61-1.974-14.101-1.974c-8.908,0-16.174,2.602-21.013,7.528c-4.369,4.442-6.737,10.636-7.054,18.44c-6.133,2.803-21.237,10.434-25.399,19.22c-2.099,4.433-10.814,8.388-17.817,11.571c-5.636,2.561-10.503,4.771-13.48,7.521c-1.598,1.476-3.766,3.669-6.267,6.202C7.681,52.688-3.083,63.58-9.476,66.273c-7.238,3.047-12.022,7.993-12.489,12.911c-0.249,2.633,0.84,5.033,2.917,6.415c3.74,2.492,17.312,4.357,26.946,4.357c2.032,0,3.994-0.077,5.819-0.237c5.479-0.503,25.312-0.905,44.213-0.905c22.609,0,32.813,0.55,34.684,1.023c3.92,0.995,7.436,0.942,12.58,0.554c3.368-0.246,7.289-0.536,13.075-0.435c5.942,0.118,11.431,0.349,15.948,0.544c4.02,0.169,7.287,0.312,9.459,0.312c1.462,0,2.392-0.06,3.031-0.178c0.235-0.03,1.45-0.127,7.174-0.127c10.246,0,26.791,0.305,26.798,0.305c2.165,0.031,3.745-1.457,4-3.467C184.779,86.538,186.425,73.199,185.158,60.775z M71.908,23.986c8.08-2.059,15.464-0.457,16.491,3.583c1.03,4.036-4.682,8.985-12.758,11.041c-8.079,2.059-15.461,0.459-16.491-3.582C58.125,30.993,63.836,26.049,71.908,23.986z M33.1,44.006c7.807-0.659,14.386,2.201,14.691,6.397c0.312,4.193-5.765,8.144-13.568,8.805c-7.809,0.66-14.387-2.201-14.691-6.4C19.223,48.61,25.297,44.671,33.1,44.006zM122.67,80.658c-0.223,0.481-0.329,0.981-0.344,1.473c-1.275-0.034-2.572-0.064-3.897-0.094c-0.921-0.019-1.791-0.024-2.629-0.024c-4.794,0-8.346,0.267-11.199,0.479c-4.11,0.311-6.946,0.45-10.042-0.332c-4.137-1.05-22.203-1.268-36.628-1.268c-19.423,0-39.159,0.41-44.93,0.938c-1.345,0.124-2.81,0.158-4.291,0.178C23.522,76.224,40.985,70.605,46.118,71.8c3.038,0.724,5.305,1.489,7.307,2.163c4.688,1.583,8.4,2.834,15.098,0.738c1.88-0.587,3.815-1.356,5.77-2.142c6.874-2.748,11.313-4.229,15.043-1.614c8.523,5.97,15.491,4.944,22.228,3.969l0.526-0.071c5.021-0.737,10.051-0.055,11.217,1.509C123.833,77.053,123.603,78.625,122.67,80.658z M111.153,57.015c-10.024,0.743-18.474-2.396-18.876-7.028c-0.406-4.629,7.39-8.989,17.41-9.734c10.022-0.752,18.479,2.397,18.881,7.027C128.974,51.904,121.175,56.27,111.153,57.015zM142.469,51.189c-1.634-5.537-4.103-11.182-7.809-14.091c-9.188-7.221-20.624-16.848-25.173-22.223C108.884,14.164,96.799,0.084,85.878-0.92c0.518-4.697,2.036-8.397,4.593-10.994c4.188-4.261,10.668-5.158,15.364-5.158c5.493,0,10.16,1.168,11.919,1.667c7.153,2.042,38.85,39.213,51.926,57.785c-4.398-1.786-9.489-2.251-15.089-0.152C148.921,44.351,145.108,47.405,142.469,51.189z M177.185,82.824c-5.475-0.083-15.981-0.236-23.304-0.236c-6.653,0-7.854,0.121-8.513,0.236c-0.301,0.047-0.774,0.07-1.361,0.079c0.029-0.512,0.06-1.065,0.077-1.571c0.924-18.863,1.812-27.383,13.295-31.685c9.708-3.65,16.787,4.983,19.528,9.136C178.086,66.809,177.729,76.612,177.185,82.824z"
let svgShapeLayer = CAShapeLayer(pathString: pizza)

// From SVG File
let svgURL = NSBundle.mainBundle().URLForResource("pizza", withExtension: "svg")
let svgShapeLayerFromFile = CAShapeLayer(SVGURL: svgURL)
Output:

This is not delicious pizza

Fill colors are read automatically if provided as an attribute on the path element. The fill attribute must be supplied as a hex value:

<path fill="#FF0066" d="M150 0 L75 200 L225 200 Z">

UIView+SVG

A UIView convenince initializer is provided as well. Same thing goes as with CAShapeLayer, you can supply a path string or an SVG file:

// From Path String
let sockPuppet = "M49.976,36.57l27.343,1.078c7.437,0,13.486-6.05,13.486-13.487s-6.049-13.487-13.485-13.487H58  c-0.429-3.546-2.45-6.235-4.881-6.235s-4.45,2.689-4.877,6.235h-4.368c-0.259,0-0.511,0.01-0.768,0.014  c-0.423-3.553-2.445-6.25-4.88-6.25c-2.719,0-4.924,3.36-4.961,7.523c-5.139,1.369-9.419,3.825-12.781,7.357  c-8.476,8.907-7.963,21.297-7.939,21.737v37.496h4.016V93.5h33.775V78.551h4.471v-1.682c0-8.529,4.16-9.612,4.639-9.708  c0.794-0.026,5.409-0.225,10.103-1.41c9.184-2.323,11.111-6.586,11.111-9.753c0-7.207-5.377-9.775-10.409-9.775  c-0.399,0-0.689,0.018-0.829,0.028H51.104l-0.237,0.017c-0.004,0.001-0.422,0.058-1.042,0.058c-5.218,0-5.218-3.253-5.218-4.322  C44.607,36.887,49.143,36.58,49.976,36.57z M87.441,24.161c0,5.583-4.542,10.124-10.057,10.125l-7.973-0.314  c-4.428-6.087-6.037-11.196-4.771-15.199c0.722-2.282,2.289-3.793,3.675-4.734h9.002C82.899,14.038,87.441,18.579,87.441,24.161z   M69.492,49.615l0.16-0.008c0.035-0.003,0.254-0.021,0.598-0.021c2.632,0,7.046,0.833,7.046,6.412c0,4.667-8.069,6.723-13.864,7.461  c-1.008-0.493-1.786-1.215-2.318-2.167c-1.917-3.429-0.405-8.997,0.517-11.677H69.492z M49.825,49.689  c0.655,0,1.153-0.047,1.384-0.074h9.357c-0.98,2.923-2.377,8.495-0.327,12.164c0.412,0.737,0.946,1.354,1.591,1.858  c-1.001,0.095-1.887,0.147-2.579,0.164l-0.153,0.012c-0.297,0.034-6.954,0.932-7.604,11.374H45.15v-7.053h-3.364v7.053H35.06v-7.053  h-3.363v7.053h-7.147v-7.053h-3.364v7.053h-5.277l-0.002-34.212c-0.005-0.115-0.447-11.52,7.037-19.36  c2.829-2.964,6.458-5.042,10.805-6.269c0.799,2.571,2.501,4.353,4.479,4.353c2.291,0,4.215-2.388,4.788-5.63  c0.291-0.005,0.565-0.031,0.86-0.031h22.781c-1.188,1.029-2.341,2.46-2.964,4.421c-1.327,4.175,0.18,9.371,4.46,15.462  l-18.109-0.714c-3.043,0-8.798,1.838-8.798,8.797C41.244,46.816,44.452,49.689,49.825,49.689z"
let svgView = UIView(pathString: sockPuppet)

// From SVG File
let svgURL = NSBundle.mainBundle().URLForResource("sockPuppet", withExtension: "svg")
let svgViewFromFile = UIView(SVGURL: svgURL)
Output:

This is not a sock puppet


SVGView

Finally, SwiftSVG provides a UIView subclass that is IBInspectable and IBDesignable. Simply add a view to your storyboard and use the SVGView subclass as you class name. Then put the name of your SVG file in your bundle in the IBInspectable property "SVGName"

Screenshot of SVGView in Interface Builder

Credits

License

SwiftSVG is released under the MIT License.

Latest podspec

{
    "name": "SwiftSVG",
    "version": "1.3.1",
    "summary": "A simple SVG parser written in Swift",
    "description": "A single pass, performant SVG parser with multiple interface options including UI/NSBezierPath, CAShapeLayer, and UI/NSView",
    "homepage": "https://github.com/mchoe/SwiftSVG",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Michael Choe": "michael@straussmade.com"
    },
    "social_media_url": "http://twitter.com/_mchoe",
    "platforms": {
        "osx": "10.10",
        "ios": "8.0"
    },
    "source": {
        "git": "https://github.com/mchoe/SwiftSVG.git",
        "tag": "v1.3.1"
    },
    "source_files": [
        "SwiftSVG",
        "SwiftSVG/**/*.{h,swift}"
    ],
    "pushed_with_swift_version": "3.0"
}

Pin It on Pinterest

Share This