mchoe / swiftsvg Goto Github PK
View Code? Open in Web Editor NEWA simple, performant, and lightweight SVG parser
License: Other
A simple, performant, and lightweight SVG parser
License: Other
I have my full project in Swift 2.3 and I can't upgrade it to Swift3
I am not able to use this library as its in Swift 3 and getting nearly 80 errors.
So u upload the previous swift 2.3 version of this library
Xcode keeps showing a runtime error when this method is used:
public convenience init(SVGData svgData: Data, parser: SVGParser? = nil, completion: ((SVGLayer) -> ())? = nil) { self.init() CALayer(SVGData: svgData, parser: parser) { [weak self] (svgLayer) in DispatchQueue.main.safeAsync { self?.nonOptionalLayer.addSublayer(svgLayer) } completion?(svgLayer) } }
Is there a way to initialize self on the main thread?
Just subclassed a regular UIView to inherit from SVGView and set the svg name but it does not render. Im using the latest version of xcode and have introduced the framework via cocoapods. am i missing something? All i get is a blank view.
I go det "Designables Error" in the storyboard inspector
Your code is not considering fill None to the path.
It's filling fully
<path style="fill:none;stroke:#000000;"
d="M90,66c0,2-2,4-4,4H14c-2,0-4-2-4-4V34c0-2,2-4,4-4h72c2,0,4,2,4,4V66z"/>
|OR|
<path fill="none" stroke="#000000"
d="M90,66c0,2-2,4-4,4H14c-2,0-4-2-4-4V34c0-2,2-4,4-4h72c2,0,4,2,4,4V66z"/>
When using this in a table view there are 2 issues
Uncommitted CATransaction. Set CA_DEBUG_TRANSACTIONS=1 in environment to debug.
So, I guess Carthage needs to look at the tag list (currently empty for this project) in order to install anything. Here's the STDOUT:
% carthage update
*** Cloning Alamofire
*** Cloning SwiftSVG
No tagged versions found for github "mchoe/SwiftSVG"
Great work, I really need to make an SVG out of UIBezierPath .
Is it possible in any way ?
Anyone else getting an error in NSBezierPath+CrossPlatform.swift
because of the attempt to import AppKit? It seems to me the class should be wrapped in
#if os(OSX)
#endif
Am I wrong?
Could you please update the pod spec to the latest commit, 1.1.3? Thank you! :-)
SwiftSVG has not currently supported open path. Will it be implemented in near future?
We are caching the SVG files from server and trying to load the same via method
UIView(SVGURL: svgURL) but it is not visible into view and also tried the sample URL and it loads. So does it need any specific format for loading image from local document directory ?
Sample Cached URL that i want to load into UIView.
file:///Users/nikhiltrivedi/Library/Developer/CoreSimulator/Devices/7B1ABC23-1B25-46EE-8520-F0AE12D2F84B/data/Containers/Data/Application/DA511A10-3772-4265-B36F-A647C7A821D0/Library/Caches/KeyDocuments/4607512156413564370.svg
I'm trying this Framework for my app. I would like to use it in my watchAp�p too. I have read that the specification of the library must support watchOS. Is it possible to do it adding this to the podspec file?
ss.watchos.deployment_target = '2.0'
I want to add fill color for SVG and get as UIImage to set it for different button
I just tried but dint work
func getSvgImgFnc(SvgImgFileVar: String, ClrVar: UIColor) -> UIImage
{
let svgURL = NSBundle.mainBundle().URLForResource(SvgImgFileVar, withExtension: "svg")
let svgShapeLayerFromFile = CAShapeLayer(SVGURL: svgURL!)
svgShapeLayerFromFile.fillColor = ClrVar.CGColor
let SvgImjVar = UIImageView()
SvgImjVar.layer.addSublayer(svgShapeLayerFromFile)
return SvgImjVar.image!
}
Great work! I really hope that you can provide us with a fully SVG 1.1 compatible library!
Feel free to use some of my SVG assets for your demos (I made them in Inkscape):
You can download the original svg file from my google drive (click on the download button):
http://goo.gl/m7TbmM
Feel free to use them, but check the fonts.. I use some fonts not on iOS by default, you should install them with the app, or replacing them with similar fonts
For an easier debug, here it is a splitted version of the two main buttons svg files, you can download the zip from here:
https://drive.google.com/file/d/0BxexooDWEb8ublZlcm5yRWpXTzQ/view?usp=sharing
If you need other SVG assets for your lib, tell me! I will do them for you at no cost. If your lib development will be completed it will save me a lot of time! :)
Can you please enable a swift3
branch, so that we may create pull requests?
No matter what we try it will draw only the first line of the artwork
let svgURL = Bundle.main.url(forResource: "sunglasses", withExtension: "svg")
let pathFromSVGFile = UIBezierPath.pathWithSVGURL(svgURL!)!
UIColor.blue.setStroke()
pathFromSVGFile.lineWidth = 3
pathFromSVGFile.stroke()
shapeLayer.path = pathFromSVGFile.cgPath
self.view.layer.addSublayer(shapeLayer)
Where the exact same file works if we use the shape layer directly.
let svgShapeLayerFromFile = CAShapeLayer(SVGURL: svgURL!)
//will work
As the title tells, I'm not able to make it work with storyboards. Entering SVGView
to a UIView doesn't present me an option to enter the SVGName. It simply doesn't detect/recognize the class. Also it's not available in the dropdown. Setting the module
manually also doesn't help.
Doing this via code i.e. import
etc works fine, just storyboard doesn't work.
Installation was via carthage so maybe this is related to #27
Hi, thanks for making a Swift SVG library! Many existing obj-c libraries are not quite Swift compatible yet and I'm glad you've begun one! That said, I'm wondering what needs to be done to support more SVG files. Is it a path versus fill support issue? I haven't yet gotten my hands dirty with SVG XML data so I don't really know what the issues are.
I've forked your repo with another SVG file for you to take a look at at your leisure. Hopefully we can show more SVGs with your library soon!
Thanks @mchoe!
Also, I thought it was odd that I had different results for both iOS and Mac demos with my supplied SVG file.
SwiftSVG crashes with the following floating point value:
"M186.999998 336.769903 L0 129.0625 L0 7.10542736e-15 L186.999998 336.769903 Z"
assertion failed: Invalid character "e" found: file SwiftSVG/SwiftSVG/String+SVG.swift, line 494
In other words, the text "35.5.8" should be interpreted (greedily) as "35.5 0.8". Reproduced against feature/2.0.0 branch.
FYI, on the README.md, both SockPuppet and Pizza examples are the same pizza SVG.
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 sockPuppet = "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"
I love this project and I love pizza, not complaining 😂🍕
I got this function to return my view with filled color
Fill color is not working
Image becomes too big and how do I resize this to fit in rect
func GetSvgViewFnc(SvgImgFileVar: String, ClrVar: UIColor) -> UIView
{
let svgURL = NSBundle.mainBundle().URLForResource(SvgImgFileVar, withExtension: "svg")
let svgShpLyrVar = CAShapeLayer(SVGURL: svgURL!)
svgShpLyrVar.fillColor = ClrVar.CGColor
svgShpLyrVar.bounds = CGRect(x: 0,y: 0,width: 24,height: 24)
let SvgVyu = UIView(frame: CGRect(x: 0,y: 0,width: 24,height: 24))
SvgVyu.layer.addSublayer(svgShpLyrVar)
return SvgVyu
}
Failed to write to /Users/A/X/Carthage/Build/Mac/SwiftSVG.framework: Error Domain=NSCocoaErrorDomain Code=260 "The file “SwiftSVG.framework” couldn’t be opened because there is no such file." UserInfo={NSURL=file:///Users/A/Library/Caches/org.carthage.CarthageKit/DerivedData/9.2_9C40b/SwiftSVG/v2.1.2/Build/Intermediates.noindex/ArchiveIntermediates/SwiftSVG/BuildProductsPath/Release-iphoneos/SwiftSVG.framework, NSFilePath=/Users/A/Library/Caches/org.carthage.CarthageKit/DerivedData/9.2_9C40b/SwiftSVG/v2.1.2/Build/Intermediates.noindex/ArchiveIntermediates/SwiftSVG/BuildProductsPath/Release-iphoneos/SwiftSVG.framework, NSUnderlyingError=0x7fe879d65390 {Error Domain=NSPOSIXErrorDomain Code=2 "No such file or directory"}}
Is it support macOS?
See https://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
When a path contains an "A" or "a" command the parser simply asserts.
I don't use Interface Builder, but UIView's convenience init(pathString: String)
and convenience init(SVGURL: NSURL)
are internal, I have no way to use them when using framework.
It seems <rect>
tags aren't part of the UIBezierPath when I parse the SVG file below :
<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 17.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg version="1.1" id="Calque_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="31.18px" height="31.18px" viewBox="0 0 31.18 31.18" enable-background="new 0 0 31.18 31.18" xml:space="preserve">
<rect x="8.621" y="11.809" width="0.875" height="7.688"/>
<rect x="10.121" y="11.809" width="0.875" height="7.688"/>
<rect x="20.184" y="11.809" width="0.875" height="7.688"/>
<rect x="21.684" y="11.809" width="0.875" height="7.688"/>
<path d="M15.574,12.783c-2.615,0-4.734,1.285-4.734,2.87s2.12,2.87,4.734,2.87s4.734-1.285,4.734-2.87S18.189,12.783,15.574,12.783z
M16.942,17.784c-0.948,0.608-2.328,0.147-3.084-1.03c-0.755-1.177-0.6-2.625,0.348-3.233s2.328-0.147,3.084,1.03
C18.046,15.728,17.89,17.176,16.942,17.784z"/>
</svg>
Could export UIBezierPath to SVG path?
What the point of using vector graphics, which you couldn't scale?
The most important part - respecting content mode (scale to fill, center, aspect to fit)
I have designed the below share icon. SwiftSVG is not rendering this.
Kindly help me.
<g stroke="#000000" stroke-width="5">
<line x1="50" y1="80" x2="80" y2="20"/>
<line x1="20" y1="20" x2="50" y2="80"/>
<circle cx="20" cy="20" r="10"/>
<circle cx="80" cy="20" r="10"/>
<circle cx="50" cy="80" r="10"/>
</g>
Error: warning: no umbrella header found for target 'SwiftSVGiOS', module map will not be generated
Possible fix? http://stackoverflow.com/questions/30355133/swift-framework-umbrella-header-h-not-found
Per the SVG 1.1 spec:
"The command letter can be eliminated on subsequent commands if the same command is used multiple times in a row (e.g., you can drop the second "L" in "M 100 200 L 200 100 L -100 -200" and use "M 100 200 L 200 100 -100 -200" instead)."
I can't figure it out how to center my SVG...
The code I'm using:
let icon = UIView(SVGNamed: "icon_english") { (svgLayer) in
svgLayer.fillColor = UIColor.white.cgColor
}
icon.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.5)
self.layout(icon).width(size).height(size).edges().center()
when using this svg it comes out in pieces fragmented on the screen attached is the svg file and the resulting image where the hair and lapel are sent in random directions
original svg
screenShot.svg.zip
Given the following code, with a known good URL to the test flowers .svg file:
if let url = Bundle.main.url(forResource: "hawaiiFlowers", withExtension: "svg")
let shape = CAShapeLayer(SVGURL: url)
assert(shape.cgPath != nil, "CAShapeLayer has no path!") // <-- ERROR HERE.
}
The problem occurs with other .svg files as well. It appears that CAShapeLayer
path is not being set. I don't fully understand the constructor code, but it appears that the path is not being set from the parser.
The good news is that the same code on UIBezierPath
does work. (The downside that no fill/stroke attributes are being recorded with a UIBezierPath, making this method of limited use for drawing SVGs with multiple color elements.)
let maybeBezier = UIBezierPath.pathWithSVGURL(url)
if let bezier = maybeBezier {
assert(bezier.cgPath != nil, "UIBezierPath has no path!") // no error here
}
Also, a suggestion: the CAShapeLayer
constructor should be failable, and return an Optional.
e.g.
guard let shape = CAShapeLayer(SVGURL: url) else {
print("Failed to create CAShapeLayer from URL \(url)")
return
}
Thanks again for all your work.
Please finalize the Swift 4 branch
I have an objective C project in which I want to use this project. Is there a way to manually install this Library ?
Hi,
The view does not render anything when I add a color on the path.
I've also tried with your example and it is the same result.
It works fine when I remove the fill
attribute.
Rendering (Preview) isn't working (I tested on the original example project from github), image only appear on real or simulator device, but not in Xcode
Also how can we resize the image? I though it would be the same as UIView size, but it can be smaller than UIView size or even bigger (out of bounds), so seems SVGView is quite useless here, it just shows original SVG file resolution
Would be better if image could take all space of UIView (not bigger) and also saved its aspect ratio and also so we could set different behaviour like for ImageView in Android
visio export to .svg format. but SwiftSVG can't display.
I added svg image and I want сhange size. It is possible?
Hi,
Can you please elaborate more on how to use it. I can't use pod.
Firstly, thank you for this project. It's invaluable, and well written and presented.
Please consider adding shadow support to CALayer
creation.
i.e. parsing and converting <filter>
and subsequent <feOffset>, <feGaussianBlur>
etc. declarations in to the relevant CA/CG calls.
e.g.
shapeLayer.shadowColor
shapeLayer.shadowOffset
shapeLayer.shadowColor
shapeLayer.shadowRadius
shapeLayer.shadowOpacity
Thank you.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.