NSTextAttachment image not showing when drawing using CoreText

For some reason, I cannot get NSTextAttachment images when drawing the main text, although the same image will display fine when an NSAttributedString is added to the UILabel.

On iOS, this rendering will give empty spaces for NSTextAttachments, for OS X a square image of placeholder [OBJECT] for each NSTextAttachment will be added instead. Is there anything else that needs to be done to render images using CoreText?

Rendering code:

CGFloat contextHeight = CGBitmapContextGetHeight(context);
CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString((__bridge CFAttributedStringRef)_attributedString);
CGPathRef path = CGPathCreateWithRect(CGRectMake(rect.origin.x,
                                                 contextHeight - rect.origin.y - rect.size.height,
                                                 rect.size.width,
                                                 rect.size.height), NULL);
CTFrameRef frame = CTFramesetterCreateFrame(framesetter, CFRangeMake(0, 0), path, NULL);
CFRelease(framesetter);
CGPathRelease(path);
CGContextSaveGState(context);
CGContextSetTextMatrix(context, CGAffineTransformIdentity);
CGContextTranslateCTM(context, 0.0f, contextHeight);
CGContextScaleCTM(context, 1.0f, -1.0f);
CTFrameDraw(frame, context);
CGContextRestoreGState(context);
CFRelease(frame);
+4
source share
1 answer

, NSTextAttachment NSAttributedString UIView/NSView. CGContext.

:

+4

All Articles