iossvgkit

how to use SVGKImage change SVG fill color?


how to use SVGKImage change SVG fill color?

SVGKImage *svgImage = [SVGKImage imageNamed:@"card.svg"];
svgImage.size = self.bounds.size;

SVGKLayeredImageView *imageLayer = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
SVGKLayer *layer = (SVGKLayer *)imageLayer.layer;
self.layer.mask = layer;

Here, I find a easy way to get the CAShapeLayer

SVGKImage *svgImage = [SVGKImage imageNamed:@"test.svg"]; CAShapeLayer *thisLayer = svgImage.CALayerTree; //thisLayer has the UIBezierPath info in test.svg,so,you can do anything with it.


Solution

  • You have to use the CALayer sub-layers to changing the color :

    SVGKImage *svgImage = [SVGKImage imageNamed:@"Anchor.svg"];
    SVGKLayeredImageView *svgImageView = [[SVGKLayeredImageView alloc] initWithSVGKImage:svgImage];
    [capturedImageView addSubview:svgImageView];
    
    CALayer* layer = svgImageView.layer;
    for (CALayer *subLayer in layer.sublayers) {
        DLog(@"%@", [subLayer class]);
    
        for (CALayer *subSubLayer in subLayer.sublayers) {
            DLog(@"%@", [subSubLayer class]);
    
            for (CALayer *subSubSubLayer in subSubLayer.sublayers) {
                DLog(@"%@", [subSubSubLayer class]);
    
                if( [subSubSubLayer isKindOfClass:[CAShapeLayer class]]){
                    CAShapeLayer* shapeLayer = (CAShapeLayer*)subSubSubLayer;
                    shapeLayer.fillColor = [UIColor redColor].CGColor;
                }
            }
        }
    }
    

    Hope this helps.

    Source : https://github.com/SVGKit/SVGKit/issues/98