Search code examples
iosiphoneuiviewcalayercgcontext

How to make the different colour appearance while layer color of the view is changing


I am doing some custom download progresses bar in side the (subclassed) UIButton (or) i can also do that in UIView, every thing is fine, now i want change the colour of the title as the progress the proceeds, for example as u can see in the below image shows what is now i am getting,

enter image description here

what i want is like below image

enter image description here

i want change the color of the title to white or some other color as download proceeds, is there any way i can do, any sample code for this.

i am achieving this by using below code

 - (void)drawInContext:(CGContextRef)progressContext
  {

     CGContextSetFillColorWithColor(progressContext, self.progressLayerColor.CGColor);
     CGMutablePathRef Path = CGPathCreateMutable();
     CGRect boundsRect = self.bounds;
     CGPathMoveToPoint(Path, NULL, boundsRect.origin.x, boundsRect.origin.y);

     CGPathAddRect(Path, NULL, CGRectMake(0, 0, self.progress, 35));
     CGPathCloseSubpath(Path);
     CGContextAddPath(progressContext, Path);
     CGContextFillPath(progressContext);
     CGContextSetBlendMode(progressContext, kCGBlendModeClear);
     CGContextFillPath(progressContext);

     CGPathRelease(Path);

  }

Solution

  • With the help of what @jrturton suggested i am able to make this kind of progress bar,


    CAShapeLayer *maskLayer = [CAShapeLayer layer];
    maskLayer.frame = _labelGreen.bounds;//it the label on top with green background and white text color
    CGMutablePathRef mutPath = CGPathCreateMutable();
    CGPathMoveToPoint(mutPath, NULL, 0, 0);
    CGPathAddRect(mutPath, NULL, CGRectMake( 0, 0, progress, _labelGreen.frame.size.height)); //as the progress cganges the width of mask layer also changes
    maskLayer.path = mutPath;//set the path
    
    _labelGreen.layer.mask = maskLayer;
    


    the resulted output will be like below image enter image description here