iosswiftsvgkit

Change SVG colour using SvgKit in iOS Swift


I am using SVGKit library to render svg into image view. Svg is rendering perfectly but the issue is I am unable to fill the SVG colour. I found few examples in Objective C those are not helpful for me. Here I am using Swift5. Please find the attached Controller code. Thanks in advance.

Code:

let namSvgImgVar: SVGKImage = SVGKImage(named: "camera")
let namImjVar: UIImage = namSvgImgVar.uiImage
imageView.image = namImjVar

Solution

  • I have created an extension for the same:

    extension SVGKImage {
    
        // MARK:- Private Method(s)
        private func fillColorForSubLayer(layer: CALayer, color: UIColor, opacity: Float) {
            if layer is CAShapeLayer {
                let shapeLayer = layer as! CAShapeLayer
                shapeLayer.fillColor = color.cgColor
                shapeLayer.opacity = opacity
            }
    
            if let sublayers = layer.sublayers {
                for subLayer in sublayers {
                    fillColorForSubLayer(layer: subLayer, color: color, opacity: opacity)
                }
            }
        }
    
        private func fillColorForOutter(layer: CALayer, color: UIColor, opacity: Float) {
            if let shapeLayer = layer.sublayers?.first as? CAShapeLayer {
                shapeLayer.fillColor = color.cgColor
                shapeLayer.opacity = opacity
            }
        }
    
        // MARK:- Public Method(s)
        func fillColor(color: UIColor, opacity: Float) {
            if let layer = caLayerTree {
                fillColorForSubLayer(layer: layer, color: color, opacity: opacity)
            }
        }
    
        func fillOutterLayerColor(color: UIColor, opacity: Float) {
            if let layer = caLayerTree {
                fillColorForOutter(layer: layer, color: color, opacity: opacity)
            }
        }
    }
    

    If your SVG image has only one layer to color use fillColor, or fillOutterLayerColor if there are multiple, you can modify it based on your need. This is how I use it:

    static func getImage(imageName: String, fontSize size: CGSize, fillColor color: UIColor?, opacity: Float = 1.0) -> UIImage? {
        let svgImage: SVGKImage = SVGKImage(named: imageName)
        svgImage.size = size
        if let colorToFill = color {
           svgImage.fillColor(color: colorToFill, opacity: opacity)
        }
    
       return svgImage.uiImage
    }