Drawing a single character with Core Text

I'm trying to rebuild a method to draw a single character as an image for an applicaiton icon. This previously worked with UIGraphics calls, but because of image orientation issues and the nil ContextRef warnings, I want to use a "cleaner" solution. I found this code to draw the text image online (credit raywenderlich.com):


CGContextRef context = UIGraphicsGetCurrentContext();

CGMutablePathRef path = CGPathCreateMutable(); //1
CGPathAddRect(path, NULL, self.bounds );

NSAttributedString* attString = [[NSAttributedString alloc]initWithString:@"A"]; //2

CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attString); //3
CTFrameRef frame =
   
CTFramesetterCreateFrame(framesetter,
       
CFRangeMake(0, [attString length]), path, NULL);

CTFrameDraw(frame, context); //4

CFRelease(frame); //5
CFRelease(path);
CFRelease(framesetter);

I've dynamically determined what I thought was a centered CGRect that will fit in my bounds. However, the text shows up VERY small at the top left corner of the image. This is probably expected as the smallest font might be the system default. But, when I change the font size to a dynamically scaled size that should maximize it in the alloted CGRect via this change to the attributed string, no text shows up at all:


UIFont* largestFont=...//determined by the largest font bounds that will fit in my image rect
attrString = [[NSAttributedString alloc] initWithString:@"A" attributes:@{NSFonrtAttributeName:largestFont,NSForegroundColorAttributeName:[UIColor blackColor]}];


What's going on here and how do I adjust the font size with CoreText? Second, is CoreText the best solution to my problem? I see all the simpler Core Graphics methods have been deprecated.

Try increasing the text size by one pixel at a time and watch what happens. My bet is that the "height" of the text isn't what you think it is and the baseline isn't in the correct position. The text is probably drawing correctly but the visible part of the letter isn't in the rect.

Thanks, it wasn't a location problem but this shows me what is happening. What I found by incrementing the font size was the font scales up to a certain point, then no longer shows up after I get past a certain size. Normally, I'm using NSString.sizeWithAttributes in a loop to find the largest font that will fit in my image rect. But, when I draw at that font size the character doesn't show up. The line height is about 69 and my image height is 75. There's plenty of space left and I'd like this one character to be drawn at around that ratio to the height.


However, when I use your increment method, I can get all the way up to a font size with a line height of 62 centered as I want before the character will start disappearing. Any clue why?


BTW, the largest font size I get up to is 38 if that means anything to you.

Accepted Answer

Not sure why this works, but reversing the Y coordinate sign on my CGRect fixed the problem. The characters appeared perfectly centered when the Y coordinate was positive with smaller font sizes, so I have no clue why this seemed to fix my problem.

Drawing a single character with Core Text
 
 
Q