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.