使用NSString的drawAtPoint方法代替CGContextShowGlyphsAtPoint问题 [英] Using NSString's drawAtPoint method in place of CGContextShowGlyphsAtPoint problems
问题描述
在我的应用程序中,我正在尝试沿路径渲染文本;这对大多数人物来说都没问题,但对于日语(或任何非mac-Roman)都没有。我被建议使用[NSString drawAtPoint],它在我的CATiledLayer中显示正确的字符;然而,它们在大约5秒钟后消失。在这段时间我可以缩放图层并且它们可以正确缩放,但它们似乎没有像文本的其余部分那样被提交到CATiledLayer。
In my app I'm trying to render text along a path; this is fine for most characters but not for Japanese (or anything non mac-Roman). I've been advised to use [NSString drawAtPoint] which displays the correct characters in my CATiledLayer; however, they dissapear after approximately 5 seconds. In this time I can zoom the layer and they scale properly, but they don't seem to get committed to the CATiledLayer like the rest of the text usually is.
之前我渲染,我检查字符串并决定是否其中任何一个都不可渲染。如果我有问题,我会使用drawAtpoint:
Before I render, I check the string and decide whether any of them will not be renderable. If I'm going to have issues, I use drawAtpoint instead:
if (!isFullyDisplayable)
{
CGContextShowGlyphsAtPoint(context, pt.x, pt.y, realGlyph, 1);
}
else {
// fall back on less flexible font rendering for difficult characters
NSString *b = [gv text];
NSString *c = [b substringWithRange:NSMakeRange(j,1)];
[c drawAtPoint:pt withFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]];
}
有没有人有任何关于为什么的指示文字消失了吗?
Does anyone have any pointers as to why the text disappears?
一旦使用了drawAtPoint,我的调试输出就会充满:
As soon as the drawAtPoint is used my debug output gets flooded with:
<Error>: CGContextGetShouldSmoothFonts: invalid context
<Error>: CGContextSetFont: invalid context
<Error>: CGContextSetTextMatrix: invalid context
<Error>: CGContextSetFontSize: invalid context
<Error>: CGContextSetTextPosition: invalid context
<Error>: CGContextShowGlyphsWithAdvances: invalid context
所以我猜测它与我的上下文管理有关,但是我认为如果它与我使用CGContextShowGlyphsAtPoint在同一个地方它应该有正确的上下文吗?
So I'm guessing it is something to do with my context management, but I assumed that if this is in the same place as I use CGContextShowGlyphsAtPoint it should have the correct context already?
推荐答案
回答我自己的问题:
NSString drawAtPoint:withFont:利用上下文堆栈,从我调用此方法的地方,堆栈是空的。用
NSString drawAtPoint:withFont: makes use of the context stack, and from where I was calling this method the stack was empty. Wrapping the call with
UIGraphicsPushContext(context); and UIGraphicsPopContext();
诀窍。
这篇关于使用NSString的drawAtPoint方法代替CGContextShowGlyphsAtPoint问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!