[英]NSTextAttachment image not showing when drawing using CoreText
出於某種原因,當使用核心文本時,我無法獲得要繪制的NSTextAttachment圖像,盡管當NSAttributedString添加到UILabel時,相同的圖像會顯示正常。
在iOS上,此渲染將為NSTextAttachments提供空白空間,對於OS X,將為每個NSTextAttachment呈現占位符[OBJECT]方形圖像。 為了使用CoreText渲染圖像,還需要做些什么嗎?
渲染代碼:
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);
原因很簡單,NSTextAttachment僅適用於將NSAttributedString呈現為UIView / NSView。 它不能用於渲染到常規CGContext中。
有兩種方法可以解決問題:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.