I'm trying to draw text using CoreText. I have the following code:
CGContextRef uiContext = UIGraphicsGetCurrentContext();
NSMutableDictionary<NSAttributedStringKey,id> *attributes = [NSMutableDictionary<NSAttributedStringKey,id> new];
[attributes setObject:UIColor.redColor forKey:NSForegroundColorAttributeName];
[attributes setObject:[UIFont systemFontOfSize:40] forKey:NSFontAttributeName];
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"Str" attributes:attributes];
CGMutablePathRef path = CGPathCreateMutable();
CGPathAddRect(path, nil, CGRectMake(0, 50, attrString.size.width, attrString.size.height));
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((CFAttributedStringRef)attrString);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, attrString.length), path, nil);
CTFrameDraw(frame, uiContext);
With the above code, text is placed properly, but is mirrored across y-direction
So, I tried to apply translate and scale transformation, which helped with text, but now it's placed at to bottom left corner, which isn't the desired behavior.
CGContextSetTextMatrix(uiContext, CGAffineTransformIdentity);
CGContextTranslateCTM(uiContext, 0.0, self.bounds.size.height);
CGContextScaleCTM(uiContext, 1.0, -1.0);
Is there any ideas what am I missing here? Thanks.