【问题标题】:iOS draws rectangles for certain fonts of text when using CGcontextshowtextatpointiOS在使用CGcontextshowtextatpoint时为某些文本字体绘制矩形
【发布时间】:2011-11-12 12:14:05
【问题描述】:

x 和 y 是整数。

当我使用 CGBitmapCreateContext 发送自定义创建的上下文,然后将其与 ArialMT 等常见字体一起使用时,以下代码可以正常工作:

int theSize = [caption.text length];
CGContextShowTextAtPoint(context, x, y, [caption.text UTF8String], theSize);

但是当我使用某些字体时,字母会显示为矩形。所以我尝试了另一个堆栈溢出答案的建议,即使用 CoreText:

 // supposedly a solution to glyphs problem
CGContextSaveGState(context);
CGContextSetRGBFillColor(context, 0, 0, 0, .5);
[caption.text drawAtPoint:CGPointMake(x, y) withFont:caption.font];
CGContextRestoreGState(context);

但这只是给我一个错误:Sep 7 13:33:31 ... : CGContextShowGlyphsWithAdvances: invalid context 0x0

【问题讨论】:

    标签: iphone objective-c fonts cgcontext


    【解决方案1】:

    矩形表示该字形在该字体中不可用。使用-drawAtPoint:withFont: 将使用字体回退来使用其他字体绘制字形,但请注意,这不是核心文本,它只是 UIStringDrawing。在任何情况下,这都会失败,因为 -drawAtPoint:withFont: 调用依赖于当前的 UI 图形上下文堆栈,但您使用的是不在堆栈上的自定义上下文。要么使用 UIGraphicsBeginImageContext[WithOptions]() 和 UIGraphicsEndImageContext() 创建和销毁你的 CGContext,要么使用 UIGraphicsPushContext() 和 UIGraphicsPopContext() 将你的上下文添加到堆栈中,然后再次删除它。

    或者,您实际上可以开始使用使用显式上下文的 CoreText(就像 CoreGraphics 一样),但这将是更多的代码行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-31
      • 1970-01-01
      相关资源
      最近更新 更多