NSGraphicsContext* newCtx = [NSGraphicsContext graphicsContextWithGraphicsPort:bitmapContext flipped:true]; [NSGraphicsContext saveGraphicsState]; [NSGraphicsContext setCurrentContext:newCtx]; NSAttributedString *string = /* make a string with all of the desired attributes */; [string drawInRect:locationToDraw];
[NSGraphicsContext restoreGraphicsState];
int charCount = [string length]; CGGlyph glyphs[charCount]; CGRect rects[charCount]; CTFontGetGlyphsForCharacters(theCTFont, (const unichar*)[string cStringUsingEncoding:NSUnicodeStringEncoding], glyphs, charCount); CTFontGetBoundingRectsForGlyphs(theCTFont, kCTFontDefaultOrientation, glyphs, rects, charCount); int totalwidth = 0, maxheight = 0; for (int i=0; i < charCount; i++) { totalwidth += rects[i].size.width; maxheight = maxheight < rects[i].size.height ? rects[i].size.height : maxheight; } dim = CGSizeMake(totalwidth, maxheight);