swhitty / SwiftDraw

Swift library and command line tool to convert SVGs into SFSymbol, PNG, PDF and Swift source code.
zlib License
326 stars 41 forks source link

Is there an easy way to override fill color? #3

Closed eonist closed 3 years ago

eonist commented 3 years ago

Like with https://github.com/mchoe/SwiftSVG you can do:

let svgURL = URL(string: "https://openclipart.org/download/181651/manhammock.svg")!
let hammock = UIView(SVGURL: svgURL) { (svgLayer) in
    svgLayer.fillColor = UIColor(red:0.52, green:0.16, blue:0.32, alpha:1.00).cgColor
    svgLayer.resizeToFit(self.view.bounds)
}
self.view.addSubview(hammock)

thanks.

swhitty commented 3 years ago

There is just by using regular UIImage.RenderingMode and tintColor.

let data = try Data(contentsOf: URL(string: "https://openclipart.org/download/181651/manhammock.svg")!)

// rasterize to UIImage
let uiImage = Image(data: data)!.rasterize()
// render as template
let templated = uiImage.withRenderingMode(.alwaysTemplate)

// set image with tintColor
imageView.image = templated
imageView.tintColor = UIColor.red

See sample branch: tint-image

Screen Shot 2021-06-06 at 9 14 59 pm
eonist commented 3 years ago

Nice! Should maybe add that example to readme? Btw. I really like how you divided the code into smaller pieces. Big fan of this kind of modularization Good job!

eonist commented 3 years ago

@swhitty Another thing. Is it possible to get boundingBox of the svg content? And the SVG view box?

My goal is to center align SVG files of logos / brands.

If yes, I can figure it out my self if you have limited time etc. Just wanted to ask before I spend hours digging around.

swhitty commented 3 years ago

Do you have sample SVGs you would like to align?

the viewbox and bounding box size do get parsed but dot exposed in the public API.

the UIimage size is calculated from these things.

eonist commented 3 years ago

With brand logos you often want to fit things to the widest side etc Because brand logos come in many aspect ratios etc. In the https://github.com/mchoe/SwiftSVG project they have: svgLayer.boundingBoxthat worked. But that project has some strange async callbacks that makes UI programming very difficult. I guess I can fork this project and open the private API's up to this use case. Its nice to know that its possible. I will report back how things goes etc. Thanks for your time 💪

eonist commented 3 years ago

To answer the question: Here are the .svg's I use: https://fontawesome.com/v5.15/icons?s=brands&m=free

swhitty commented 3 years ago

The viewBox and bounding box really just work together for form the UIImage size, you should be able to align any UIImage by ensuring all images share the same centerPoint, and sizing is preserved. It looks like all of those images share the same height (512) but have differing widths. We can then align by ensuring the images are placed within an ImageView that has a constant height. like so;

https://github.com/swhitty/SwiftDraw/compare/brands

animation

eonist commented 3 years ago

Wow thanks for the thorough answer and example code! Will definitely pursue this very soon (1 week) and report back how it worked out. Stay tuned. Would love to use svgs in buttons and wherever icons are needed. PDF's are so timeconsuming to add. I dont know why apple hasn't added support for svgs as assets.

swhitty commented 3 years ago

I suppose the problem is SVGs are huge standard and difficult to fully support — SwiftDraw has many unsupported areas but I try and support much of what is useful to my needs.

iOS 13+ supports SF Symbols which are actually made up of SVGs. I have never tried to make my own custom symbols (because it is complicated) but there is a guide here; https://developer.apple.com/documentation/uikit/uiimage/creating_custom_symbol_images_for_your_app

eonist commented 3 years ago

Yeah. I looked at SFSymbol way before. My brain exploded. Thanks for the help so far. Will let you know how things pan out.