Easily convert your SVG files into CGPaths, CAShapeLayers, and UIBezierPaths
Objective-C++ Objective-C Swift Shell Ruby
Clone or download
Latest commit 2e15e6f Apr 7, 2018
Permalink
Failed to load latest commit information.
.github Update pull_request_template.md Apr 4, 2018
Demos migrate macOS project to Swift 4 Apr 5, 2018
PocketSVG.xcodeproj add tests Apr 4, 2018
PocketSVGTests set continueAfterFailure = false on unit tests Apr 5, 2018
.gitignore add DS_Store to .gitignore" Feb 27, 2017
.travis.yml update ci scripts Apr 7, 2018
CHANGELOG.md add tests Apr 4, 2018
Framework-Info-Mac.plist SVGExample: link framework into targets Jun 6, 2016
Framework-Info-iOS.plist SVGExample: link framework into targets Jun 6, 2016
LICENSE Update LICENSE Apr 19, 2017
PocketSVG.h replace SVGExample with Demos Aug 23, 2016
PocketSVG.podspec add changelog, update demo and docs Apr 4, 2018
README.md Update README.md Apr 6, 2018
SVGBezierPath.h further fixes to documentation warnings and make documentation consis… Aug 26, 2017
SVGBezierPath.mm remove unneeded copy Mar 5, 2018
SVGColors.plist add changelog, update demo and docs Apr 4, 2018
SVGEngine.h further fixes to documentation warnings and make documentation consis… Aug 26, 2017
SVGEngine.mm Add support for named SVG colors (#119) Mar 29, 2018
SVGImageView.h further fixes to documentation warnings and make documentation consis… Aug 26, 2017
SVGImageView.m Simplified layer/view api and made it more flexible Aug 17, 2017
SVGLayer.h further fixes to documentation warnings and make documentation consis… Aug 26, 2017
SVGLayer.m Retain CGColorRefs Mar 19, 2018
SVGPortability.h replace SVGExample with Demos Aug 23, 2016
ci.sh update ci scripts Apr 7, 2018

README.md

PocketSVG

CocoaPods Build Status CocoaPods Compatible Carthage compatible Code Coverage code size license

A simple toolkit for displaying and manipulating SVGs on iOS and macOS in a performant manner.

The goal of this project is not to be a fully compliant SVG parser/renderer. But rather to use SVG as a format for serializing CG/UIPaths, meaning it only supports SVG features that can be represented by CG/UIPaths.

Thoroughly documented.

Features

  • Support for SVG elements: path, polyline, polygon, rect, circle, ellipse
  • Support for SVG named colors.
  • Fully working iOS and macOS demos.
  • Straightforward API for typical SVG rendering as a UIImageView/NSImageView or CALayer subclass.
  • Access every shape within your SVG as a CGPath for more fine-grained manipulation.

Installation

Cocoapods

Add this to your Podfile:

pod 'PocketSVG', '~> 2.0'

Then run pod install

Carthage

Add this to your Cartfile:

github "pocketsvg/PocketSVG" ~> 2.0

Then run carthage update

Manual

Drag and drop PocketSVG.xcodeproj into your Xcode project. In your project settings, add PocketSVG.framework in Link Binary With Libraries.

Usage

Render an SVG file using SVGImageView:

let url = Bundle.main.url(forResource: "tiger", withExtension: "svg")!
let svgImageView = SVGImageView.init(contentsOf: url)
svgImageView.frame = view.bounds
svgImageView.contentMode = .scaleAspectFit
view.addSubview(svgImageView)

Output image

Manually render each path of an SVG file into using CAShapeLayers:

view.backgroundColor = .white

let svgURL = Bundle.main.url(forResource: "tiger", withExtension: "svg")!
let paths = SVGBezierPath.pathsFromSVG(at: svgURL)
let tigerLayer = CALayer()
for (index, path) in paths.enumerated() {
    let shapeLayer = CAShapeLayer()
    shapeLayer.path = path.cgPath
    if index%2 == 0 {
        shapeLayer.fillColor = UIColor.black.cgColor
    }
    else if index%3 == 0 {
        shapeLayer.fillColor = UIColor.darkGray.cgColor
    }
    else {
        shapeLayer.fillColor = UIColor.gray.cgColor
    }
    tigerLayer.addSublayer(shapeLayer)
}

var transform = CATransform3DMakeScale(0.4, 0.4, 1.0)
transform = CATransform3DTranslate(transform, 200, 400, 0)
tigerLayer.transform = transform
view.layer.addSublayer(tigerLayer)

Output image

Contributing

Bug Reports & Feature Requests

Please use the issue tracker to report any bugs or file feature requests.

Developing

PRs are welcome.