5

奇怪的情况 - 来自苹果的例子有效,但在我稍微改变它们之后,没有显示文本。这段代码正确地绘制了蓝色背景,但无论我做什么都拒绝在其上绘制文本:

#import <UIKit/UIKit.h>

@interface CWnd : UIWindow @end
@implementation CWnd

- (void) drawRect : (CGRect) i_poRect
{
  // This is working : windows is blue.
  CGContextRef oContex = UIGraphicsGetCurrentContext();
  CGContextSetRGBFillColor( oContex, 0, 0, 255, 1 );
  CGContextFillRect( oContex, i_poRect );
  // This is not working : not text is displayed.
  CGContextSelectFont( oContex, "Monaco", 10, kCGEncodingFontSpecific );
  CGContextSetRGBStrokeColor( oContex, 255, 0, 0, 1 ); 
  CGContextSetRGBFillColor( oContex, 255, 0, 0, 1 ); 
  CGContextSetTextDrawingMode( oContex, kCGTextFill );
  CGContextSetTextPosition( oContex, 100, 100 );
  CGContextShowText( oContex, "abc", 3 );
}

@end

@interface CDelegate : NSObject <UIApplicationDelegate> @end
@implementation CDelegate

- (void)applicationDidFinishLaunching : (UIApplication *) i_poApp
{
  CGRect oRect = [ [ UIScreen mainScreen ] bounds ];
    [ [ [ CWnd alloc] initWithFrame : oRect ] makeKeyAndVisible ];
}

@end

int main(int argc, char *argv[])
{    
  return UIApplicationMain( argc, argv, nil, @"CDelegate" );
}
4

6 回答 6

5

我已经毫无问题地使用以下代码将文本直接绘制到 Quartz 上下文:

CGContextSetStrokeColorWithColor(context, strokeColor); 
CGContextSetFillColorWithColor(context, strokeColor);

CGContextSelectFont(context, "Helvetica", fontSize, kCGEncodingMacRoman);
CGContextSetTextDrawingMode(context, kCGTextFill);
CGContextSetTextPosition(context, 0.0f, round(fontSize / 4.0f));
CGContextShowText(context, [text UTF8String], strlen([text UTF8String]));

在你的情况下出了什么问题并不明显。在这种情况下需要注意两件事:由于 UIView 及其 CALayers 的坐标空间翻转,文本可能会颠倒绘制,并且正如 Rhythmic Fistman 指出的那样,这不处理 UTF 编码。

一种更好但性能较差的方法是执行以下操作:

CGContextSetFillColorWithColor(context, strokeColor);
[text drawAtPoint:CGPointMake(0.0f, 0.0f) withFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];
于 2009-07-12T12:33:36.280 回答
3

嘿,我刚刚在我的代码中发现了一个注释。

显然CGContextShowTextAtPoint不适用于CGContextSetFont.
您需要改为使用CGContextSelectFont. 很明显吧?

无论如何,我知道情况并非如此,但您可以尝试使用CGContextShowTextAtPoint.

这可能不属于单独的答案。

于 2009-07-12T12:40:01.307 回答
1

其他人(这里这里)已经做了一些仿射变换并使用该CGContextShowTextAtPoint方法使其工作:

CGContextSelectFont(oContex, @"Monaco", 10, kCGEncodingFontSpecific);
CGContextSetTextDrawingMode(oContex, kCGTextFill);
CGContextSetRGBFillColor(oContex, 1.0, 0.0, 0.0, 1.0);
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(oContex, xform);
CGContextShowTextAtPoint(oContex, 100, 100, "abc", 3);

另一个可能的问题是该CGContextSetRGBFillColor方法采用 0.0 到 1.0 范围内的浮点参数。您使用 255 表示“全彩”,这不是该方法所期望的。如果您对 0 到 255 范围更满意,以下应该可以工作:

CGContextSetRGBFillColor(oContex, 255/255.0, 0/255.0, 0/255.0, 1.0);
于 2009-07-12T08:57:12.453 回答
1

嘿,可能是编码问题。

尝试更改kCGEncodingFontSpecifickCGEncodingMacRoman.

稍后当你让它全部工作时,摆脱CGContextShowTextAtPoint.
它是垃圾,非 unicode 并且要使用字形版本,您需要
调用被禁止的 API。你最好使用 UIKit 进行文本绘制。 它精通 unicode,并且还可以对字体中不存在的
字符进行不错的字形替换。

于 2009-07-12T10:04:34.330 回答
1

还有另一种可能性,即您指定的文本位置位于您尝试绘制的矩形之外。通过替换很容易测试这个

CGContextSetTextPosition( oContex, 100, 100 );

CGContextSetTextPosition( oContex, CGRectGetMidX(i_poRect), CGRectGetMidY(i_poRect));
于 2010-06-04T18:33:30.937 回答
1

使用 CGContextSetTextMatrix 方法。“颠倒问题”来自矩阵。

于 2011-09-23T02:39:15.853 回答