iPhone:如何在窗口中绘制文本?

iPhone:如何在窗口中绘制文本?,iphone,quartz-2d,Iphone,Quartz 2d,奇怪的情况-来自apple works的示例,但在我稍作更改后,文本不会显示。这段代码正确地绘制了蓝色背景,但无论我做什么,都拒绝在其上绘制文本: #import <UIKit/UIKit.h> @interface CWnd : UIWindow @end @implementation CWnd - (void) drawRect : (CGRect) i_poRect { // This is working : windows is blue. CGContext

奇怪的情况-来自apple works的示例,但在我稍作更改后,文本不会显示。这段代码正确地绘制了蓝色背景,但无论我做什么,都拒绝在其上绘制文本:

#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" );
}
#导入
@接口CWnd:UIWindow@end
@实现CWnd
-(void)drawRect:(CGRect)i_poRect
{
//这是工作:窗口是蓝色的。
CGContextRef oContex=UIGraphicsGetCurrentContext();
CGContextSetRGBFillColor(oContex,0,0,255,1);
CGContextFillRect(oContex,i_poRect);
//这不起作用:不显示文本。
CGContextSelectFont(oContex,“摩纳哥”,10,kCGEncodingFontSpecific);
CGContextSetRGBStrokeColor(oContex,255,0,0,1);
CGContextSetRGBFillColor(oContex,255,0,0,1);
CGContextSetTextDrawingMode(oContex、kCGTextFill);
CGContextSetTextPosition(oContex,100100);
CGContextShowText(oContex,“abc”,3);
}
@结束
@接口CDelegate:NSObject@end
@实现CDelegate
-(无效)ApplicationIDFinishLaunching:(UIApplication*)i_poApp
{
CGRect oRect=[[UIScreen mainScreen]界限];
[[CWnd alloc]initWithFrame:oRect]makeKeyAndVisible];
}
@结束
int main(int argc,char*argv[])
{    
返回UIApplicationMain(argc、argv、nil、@“CDelegate”);
}
其他人(和)进行了一些仿射变换,并使用了
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);

嘿,可能是编码问题

尝试将
kCGEncodingFontSpecific
更改为
kCGEncodingMacRoman

稍后,当一切正常时,请删除
CGContextShowTextAtPoint

这是垃圾,非unicode,要使用glyph版本,您需要
调用禁止的API。您最好使用UIKit进行文本绘制。
它精通unicode,还可以对字符进行象形文字替换

您的字体中不存在的内容。

我使用了以下代码,可以毫无问题地将文本直接绘制到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及其Calayer的坐标空间翻转,文本可能会颠倒绘制,正如节奏Fistman所指出的,这不处理UTF编码

一种更好但性能较差的方法是:

CGContextSetFillColorWithColor(context, strokeColor);
[text drawAtPoint:CGPointMake(0.0f, 0.0f) withFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];

嘿,我刚在我的密码里找到一个便条

显然,
CGContextShowTextAtPoint
不适用于
CGContextSetFont

您需要改用
CGContextSelectFont
。很明显,嗯

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


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

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

CGContextSetTextPosition( oContex, 100, 100 );


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

将255更改为1.0f没有帮助。添加转换没有帮助。也不起作用。顺便说一句,你可以复制和粘贴我的代码,它不需要一个项目或什么,只需编译一个.m文件,运行它,然后自己看看:)这里没有编译器。你有没有在一个更传统的项目中尝试过这段代码,比如一个带有.xib文件的项目?如果这段代码与一个.xib文件一起工作,而没有它就不能工作,那将是一个毁灭性的黑魔法。当然,我必须知道它为什么不起作用:(CGContextSelectFont是我从一开始就使用的函数。它完全不起作用:)而且我找不到一个简单的苹果示例,足以让它完全降解成这样的代码。顺便说一句,UTF-8中的英文文本与ASCII中的英文文本是二进制兼容的:(.CGContextSelectFont对AppleGothic这样的字体不起作用。你知道如何解决这个问题吗?现在我看,我不相信Monaco是iPhone上受支持的字体。试试Helvetica,看看它画的是否正确:你是对的。没有名为“Monaco”的字体-我在MacOS开发PC上试过两种最常用的字体,但猜不到。这是一种字体不过,我认为,如果提供了未列出的字体,CGContextSelectFont不会引发异常:(。它可能最终在调试版本中断言。
CGContextSetTextPosition( oContex, CGRectGetMidX(i_poRect), CGRectGetMidY(i_poRect));