---
title: "#198: Parsing \u0026 Rendering SVGs with SwiftSVG 🎨"
url: https://littlebitesofcocoa.com/198-parsing-rendering-svgs-with-swiftsvg
author: Jake Marsh
published: 2016-02-24
topics: ["Libraries","UIKit"]
description: "Vector graphics are fantastic. They give us small, compressible files, crispy rendered pixels at any scale, and they're supported by many different kinds of..."
---

# #198: Parsing & Rendering SVGs with SwiftSVG 🎨

By Jake Marsh, February 24, 2016. HTML: https://littlebitesofcocoa.com/198-parsing-rendering-svgs-with-swiftsvg

<img src='https://media.littlebitesofcocoa.com/bite-assets/198-parsing-and-rendering-svgs-with-swiftsvgs-example.png' style='max-width: 239px; float: right; margin-left: 20px; margin-bottom: 20px;' />

[Vector graphics](https://en.wikipedia.org/wiki/Vector_graphics) are fantastic. They give us [small, compressible files](https://www.google.com/search?q=svg+files+sizes&oq=svg+files+sizes&aqs=chrome..69i57j0l5.3598j0j7&sourceid=chrome&es_sm=91&ie=UTF-8), [crispy](https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/shape-rendering) rendered pixels at any scale, and they're supported by [many](https://www.sketchapp.com/) [different](http://www.adobe.com/products/illustrator.html) kinds of software and frameworks.

Except **iOS** 😭, which doesn't support them out of the box. Today we'll try a library from [Michael Choe](https://github.com/mchoe) called [SwiftSVG](https://github.com/mchoe/SwiftSVG) which provides great support for parsing and rendering **SVG** files. Let's take a look.


[SwiftSVG](https://github.com/mchoe/SwiftSVG) supports a ton of different ways of getting **SVGs** into our **app**.

``` swift
CAShapeLayer(SVGURL: SVGURL)
```

``` swift
UIView(SVGURL: SVGURL)
```

``` swift
UIView(pathString: deathStarBlueprints)
```

``` swift
CAShapeLayer(pathString: superSecretNewLogoIdea)
```

``` swift
UIBezierPath(pathString: "M150 0 L75 200 L225 200 Z")
```

[SwiftSVG](https://github.com/mchoe/SwiftSVG) also provides a custom **UIView** **subclass** we can use  in **Interface Builder** that supports **IBInspectable**/**IBDesignable**.

<img src='https://media.littlebitesofcocoa.com/bite-assets/198-parsing-and-rendering-svgs-with-swiftsvgs-ib.png' style='max-width: 226px; float: left; margin-right: 20px; margin-bottom: 20px;' />

More broadly, it's important to think about the <i>reasons why</i> we might want to use **SVGs** in our **app**. **SVGs** are essentially **XML** files <i>(Open one up in a text editor, you'll see.)</i>

They are instructions on how to <i>draw</i> a set of **paths**. The files are incredibly small.

Getting a good workflow going for creating them at-will can be tricky, [but Sketch (eventually) makes it mostly painless](https://medium.com/sketch-app-sources/exploring-ways-to-export-clean-svg-icons-with-sketch-the-correct-way-752e73ec4694#.kl6hiokai).

Once we have this, we can build some <i>awesome</i> abstractions. Imagine a **Swift struct** with `SVGName`, `color`, and `size` that can produces a **UIImage**.

``` swift
struct SVGImage {
  let SVGName: String
  let size: CGSize
  let color: UIColor

  func asImage() -> UIImage {
    // Neato!
  }
}
```

More info about _SwiftSVG_ can be found at [git.io/swiftsvg](http://git.io/swiftsvg)
