Here is the code that I ran and tested that will work for you. See comments in more details:
Update: I removed the manualYOffset: parameter. Now it calculates to center the text vertically in the circle. Enjoy it!
- (void)drawRect:(CGRect)rect { // Make sure the UIView 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 can still see the text):

Christian di lorenzo
source share