使用NSString的drawAtPoint方法代替CGContextShowGlyphsAtPoint问题

dav*_*ryn 10 iphone cgcontext catiledlayer

在我的应用程序中,我试图沿着路径渲染文本; 这对大多数人物来说都没问题,但对于日语(或任何非mac-Roman)都没有.我被建议使用[NSString drawAtPoint],它在我的CATiledLayer中显示正确的字符; 然而,它们在大约5秒后消失.在这个时候我可以缩放图层并且它们可以正确缩放,但它们似乎没有像文本的其他部分那样被提交到CATiledLayer.

在渲染之前,我检查字符串并确定它们中的任何一个是否都不可渲染.如果我有问题,我会改用drawAtpoint:

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]];


 }
Run Code Online (Sandbox Code Playgroud)

有没有人有关于为什么文本消失的指示?

一旦使用drawAtPoint,我的调试输出就会充满:

<Error>: CGContextGetShouldSmoothFonts: invalid context
<Error>: CGContextSetFont: invalid context
<Error>: CGContextSetTextMatrix: invalid context
<Error>: CGContextSetFontSize: invalid context
<Error>: CGContextSetTextPosition: invalid context
<Error>: CGContextShowGlyphsWithAdvances: invalid context
Run Code Online (Sandbox Code Playgroud)

所以我猜测它与我的上下文管理有关,但我认为如果这与我使用CGContextShowGlyphsAtPoint在同一个地方它应该有正确的上下文?

dav*_*ryn 17

回答我自己的问题:

NSString drawAtPoint:withFont:利用上下文堆栈,从我调用此方法的地方来看,堆栈是空的.用来包装电话

UIGraphicsPushContext(context); and UIGraphicsPopContext();
Run Code Online (Sandbox Code Playgroud)

做了伎俩.