Search code examples
iosdrawingcore-graphicsclipping

Drawing a path with subtracted text using Core Graphics


Creating filled paths in Core Graphics is straight-forward, as is creating filled text. But I am yet to find examples of paths filled EXCEPT for text in a sub-path. My experiments with text drawing modes, clipping etc have got me nowhere.

Here's an example (created in photoshop). How would you go about creating the foreground shape in Core Graphics?

Example of text subtracted from path (created in photoshop)

I would mention that this technique appears to be used heavily in an upcoming version of a major mobile OS, but I don't want to fall afoul of SO's NDA-police ;)


Solution

  • Here's some code I ran and tested that will work for you. See the inline comments for details:

    Update: I've removed the manualYOffset: parameter. It now does a calculation to center the text vertically in the circle. Enjoy!

    - (void)drawRect:(CGRect)rect {
        // Make sure the UIView's background is set to clear either in code or in a storyboard/nib
    
        CGContextRef context = UIGraphicsGetCurrentContext();
    
        [[UIColor whiteColor] setFill];
        CGContextAddArc(context, CGRectGetMidX(rect), CGRectGetMidY(rect), CGRectGetWidth(rect)/2, 0, 2*M_PI, YES);
        CGContextFillPath(context);
    
        // Manual offset may need to be adjusted depending on the length of the text
        [self drawSubtractedText:@"Foo" inRect:rect inContext:context];
    }
    
    - (void)drawSubtractedText:(NSString *)text inRect:(CGRect)rect inContext:(CGContextRef)context {
        // Save context state to not affect other drawing operations
        CGContextSaveGState(context);
    
        // Magic blend mode
        CGContextSetBlendMode(context, kCGBlendModeDestinationOut);
    
        // This seemingly random value adjusts the text
        // vertically so that it is centered in the circle.
        CGFloat Y_OFFSET = -2 * (float)[text length] + 5;
    
        // Context translation for label
        CGFloat LABEL_SIDE = CGRectGetWidth(rect);
        CGContextTranslateCTM(context, 0, CGRectGetHeight(rect)/2-LABEL_SIDE/2+Y_OFFSET);
    
        // Label to center and adjust font automatically
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, LABEL_SIDE, LABEL_SIDE)];
        label.font = [UIFont boldSystemFontOfSize:120];
        label.adjustsFontSizeToFitWidth = YES;
        label.text = text;
        label.textAlignment = NSTextAlignmentCenter;
        label.backgroundColor = [UIColor clearColor];
        [label.layer drawInContext:context];
    
        // Restore the state of other drawing operations
        CGContextRestoreGState(context);
    }
    

    Here's the result (you can change the background to anything and you'll still be able to see through the text):

    Result