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

发布于 2024-07-27 14:42:09 字数 1199 浏览 3 评论 0原文

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

#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" );
}

Strange situation - examples from apple works, but after i change them a bit, text is not displayed. This bit of code correctly draws blue background but refuses to draw text on it no matter what i do:

#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" );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

辞取 2024-08-03 14:42:09

我已经使用以下代码毫无问题地将文本直接绘制到 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]];

I've used the following code without problem to draw text directly to a Quartz context:

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

It's not obvious what's going wrong in your case. There are two things to watch out for in this case: the text may draw upside-down due to the flipped coordinate space of UIViews and their CALayers, and as Rhythmic Fistman points out, this doesn't handle UTF encodings.

A better, although less performant, approach is to do something like:

CGContextSetFillColorWithColor(context, strokeColor);
[text drawAtPoint:CGPointMake(0.0f, 0.0f) withFont:[UIFont fontWithName:@"Helvetica" size:fontSize]];
梦里人 2024-08-03 14:42:09

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

显然 CGContextShowTextAtPoint 不适用于 CGContextSetFont
您需要改用CGContextSelectFont。 很明显,是吧?

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

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

Hey, I just found a note in my code.

Apparently CGContextShowTextAtPoint doesn't work with CGContextSetFont.
You need to instead use CGContextSelectFont. Obvious, huh?

Anyway, I know this isn't the case, but you could try using CGContextShowTextAtPoint.

This probably didn't belong in a separate answer.

神经大条 2024-08-03 14:42:09

其他人(此处此处)完成了一些仿射变换并使用了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);

Other people (here and here) have done some affine transformations and used the CGContextShowTextAtPoint method in order to make it work:

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

One other possible problem is that the CGContextSetRGBFillColor method takes floating-point arguments in the range 0.0 to 1.0. You are using 255 to indicate "full color", which is not what that method expects. The following should work, if you are more comfortable with the 0 to 255 range:

CGContextSetRGBFillColor(oContex, 255/255.0, 0/255.0, 0/255.0, 1.0);
内心荒芜 2024-08-03 14:42:09

嘿嘿,可能是编码问题。

尝试将 kCGEncodingFontSpecific 更改为 kCGEncodingMacRoman

稍后,当您一切正常后,请摆脱 CGContextShowTextAtPoint
它是垃圾,非 unicode,要使用您需要的字形版本
调用禁止的 API。 使用 UIKit 进行文本绘制会更好。
它精通 unicode,并且还可以对字符进行适当的字形替换
您的字体中不存在这些内容。

Hey, could be an encoding problem.

Try changing kCGEncodingFontSpecific to kCGEncodingMacRoman.

Later when you get it all working, get rid of CGContextShowTextAtPoint.
It's garbage, non-unicode and to use the glyph versions you'll need to
call forbidden APIs. You're way better off using UIKit for text drawing.
It's unicode savvy and also does decent glyph substitution for characters
that aren't present in your font.

放飞的风筝 2024-08-03 14:42:09

还有另一种可能性,那就是您指定的文本位置位于您尝试绘制的矩形之外。 替换可以很容易地测试这一点

CGContextSetTextPosition( oContex, 100, 100 );

通过

CGContextSetTextPosition( oContex, CGRectGetMidX(i_poRect), CGRectGetMidY(i_poRect));

There is one other possibility and that is that the text position you specify lies outside the rectangle in which you are trying to draw. It's easy to test this by substituting

CGContextSetTextPosition( oContex, 100, 100 );

with

CGContextSetTextPosition( oContex, CGRectGetMidX(i_poRect), CGRectGetMidY(i_poRect));
初见 2024-08-03 14:42:09

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

use CGContextSetTextMatrix method. The "upside down problem" is from matrix.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文