7

我一直在尝试使用 Quartz 上下文显示文本,但无论我尝试了什么,我都没有运气让文本显示(尽管我能够显示各种其他 Quartz 对象)。有人知道我可能做错了什么吗?

例子:

-(void)drawRect:(CGRect)rect
{   
  // Drawing code
  CGContextRef  context = UIGraphicsGetCurrentContext();
  CGContextSelectFont(context, "Arial", 24, kCGEncodingFontSpecific);
  CGContextSetTextPosition(context,80,80);
  CGContextShowText(context, "hello", 6);
  //not even this works
  CGContextShowTextAtPoint(context, 1,1, "hello", 6);
}    
4

2 回答 2

9

好,我知道了。首先,将您的编码模式更改为 kCGEncodingMacRoman。其次,在它下面插入这一行:

CGContextSetTextMatrix(canvas, CGAffineTransformMake(1, 0, 0, -1, 0, 0));

这会设置文本的转换矩阵,以便正确绘制。如果您不输入该行,则您的文本将上下颠倒并从后向前。不知道为什么这不是默认设置。最后,确保您设置了正确的填充颜色。如果您忘记将背景颜色更改为文本颜色并最终得到白底白字,则很容易犯错误。

于 2010-08-11T13:13:48.407 回答
7

这是我正在使用的代码片段。

UIColor *mainTextColor = [UIColor whiteColor];
[mainTextColor set];
drawTextLjust(@"Sample Text", 8, 50, 185, 18, 16);

和:

static void drawTextLjust(NSString* text, CGFloat y, CGFloat left, CGFloat right,
                          int maxFontSize, int minFontSize) {
    CGPoint point = CGPointMake(left, y);
    UIFont *font = [UIFont systemFontOfSize:maxFontSize];
    [text drawAtPoint:point forWidth:right - left withFont:font
       minFontSize:minFontSize actualFontSize:NULL
       lineBreakMode:UILineBreakModeTailTruncation
       baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
}
于 2008-09-27T10:04:29.770 回答