CGContextRef context = UIGraphicsGetCurrentContext();
// Flip the coordinate system
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context,
0, self.bounds.size.height);
CGContextScaleCTM(context,
1.0, -
1.0);
// Create a path to render text in
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, NULL, self.bounds );
// An attributed string containing the text to render
NSAttributedString* attString = [[NSAttributedString alloc]
initWithString:...];
// Create a path to wrap around
CGMutablePathRef clipPath = CGPathCreateMutable();
CGPathAddEllipseInRect(clipPath, NULL, CGRectMake(
200,
200,
300,
300) );
// A CFDictionary containing the clipping path
CFStringRef keys[] = { kCTFramePathClippingPathAttributeName };
CFTypeRef values[] = { clipPath };
CFDictionaryRef clippingPathDict = CFDictionaryCreate(NULL,
(
constvoid **)&keys, (
constvoid **)&values,
sizeof(keys) /
sizeof(keys[
0]),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
// An array of clipping paths -- you can use more than one if needed!
NSArray *clippingPaths = [NSArray arrayWithObject:(NSDictionary*)clippingPathDict];
// Create an options dictionary, to pass in to CTFramesetter
NSDictionary *optionsDict = [NSDictionary dictionaryWithObject:clippingPaths forKey:(NSString*)kCTFrameClippingPathsAttributeName];
// Finally create the framesetter and render text
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString);
//3
CTFrameRef frame = CTFramesetterCreateFrame(framesetter,
CFRangeMake(
0, [attString length]), path, optionsDict);
CTFrameDraw(frame, context);
// Clean up
CFRelease(frame);
CFRelease(path);
CFRelease(framesetter);