如何在 iPhone 上旋转 Quartz 绘制的文本

发布于 2024-08-14 19:46:01 字数 873 浏览 5 评论 0原文

我想用 Quartz 绘制一些文本。现在应用程序绘制 替代文本,但我希望它显示为“0123456789”。有什么办法可以显示其正常方向吗?

这是我的代码:

    //set image view
 drawImage = [[UIImageView alloc] initWithImage:nil];
 drawImage.frame = self.view.frame;
 [self.view addSubview:drawImage];

 UIGraphicsBeginImageContext(self.view.frame.size);  // begin
 // current context
 CGContextRef context = UIGraphicsGetCurrentContext();

 CGContextSelectFont(context, "Courier", 40,  kCGEncodingMacRoman);
 CGFloat white[] = {1.0, 1.0, 1.0, 1.0};
 CGContextSetFillColor(context, white);
 CGContextShowTextAtPoint(context, 0, 30, "0123456789", strlen("0123456789"));

 // get image
 drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();    // end

I want to draw some texts using Quartz. Now the app draws
alt text
, but I want it to show as "0123456789". Is there any way to show its normal orientation?

Here is my code:

    //set image view
 drawImage = [[UIImageView alloc] initWithImage:nil];
 drawImage.frame = self.view.frame;
 [self.view addSubview:drawImage];

 UIGraphicsBeginImageContext(self.view.frame.size);  // begin
 // current context
 CGContextRef context = UIGraphicsGetCurrentContext();

 CGContextSelectFont(context, "Courier", 40,  kCGEncodingMacRoman);
 CGFloat white[] = {1.0, 1.0, 1.0, 1.0};
 CGContextSetFillColor(context, white);
 CGContextShowTextAtPoint(context, 0, 30, "0123456789", strlen("0123456789"));

 // get image
 drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
 UIGraphicsEndImageContext();    // end

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

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

发布评论

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

评论(4

缺⑴份安定 2024-08-21 19:46:01

在 Quartz 中,图像会上下颠倒,因为它的 Y 轴是倒置的。

iPhone 的 Y 轴从上到下为正,即原点位于左上角,而在 Quartz 和 OpenGL 中,坐标与良好的旧几何体相同,即原点位于左下角。

这里是一些解决 Quartz 反转问题的代码:

CGContextRef ctx = UIGraphicsGetCurrentContext();
// Tanslate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(ctx, 0.0, rect.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

这是一篇关于它的有趣文章,作者说新加坡的一个团队由于无法解决这个反转问题而放弃了 iPhone 项目:http://trandangkhoa.blogspot.com/2009/07/iphone-os-drawing -image-and-stupid.html

In Quartz, the image will be upside down because its Y-Axis is inverted.

iPhone's Y axis goes positive from top to bottom i.e. origin is at top left while in Quartz and OpenGL, the co-ordinates are the same way as good old geometry i.e. origin at bottom left.

Here is some code for solving Quartz inversion problem:

CGContextRef ctx = UIGraphicsGetCurrentContext();
// Tanslate and scale upside-down to compensate for Quartz's inverted coordinate system
CGContextTranslateCTM(ctx, 0.0, rect.size.height);
CGContextScaleCTM(ctx, 1.0, -1.0);

Here is an interesting post about it where the author says a team in Singapore gave up an iPhone project due to this inversion problem which they could not figure out: http://trandangkhoa.blogspot.com/2009/07/iphone-os-drawing-image-and-stupid.html

青春有你 2024-08-21 19:46:01

处理这个问题的另一种方法是用类似

[text drawAtPoint:CGPointMake(0.0f, 30.0f) withFont:[UIFont fontWithName:@"Courier" size:40.0f]];

文本是 NSString 的代码替换 Quartz 文本绘制代码(从 CGSelectFont() 到 CGContextShowTextAtPoint ())。在 iPhone 上,-drawAtPoint:withFont: 自动翻转文本以考虑 UIViews 的反向 Y 轴。

与纯 Quartz 文本绘图中的 MacRoman 编码相比,这种方法具有处理更广泛字符集的优势。不过,速度稍微慢一些。

Another way to handle this would be to replace the Quartz text drawing code (from CGSelectFont() to CGContextShowTextAtPoint ()) with something like

[text drawAtPoint:CGPointMake(0.0f, 30.0f) withFont:[UIFont fontWithName:@"Courier" size:40.0f]];

where text is an NSString. On the iPhone, -drawAtPoint:withFont: automatically flips the text to account for the inverted Y axis of the UIViews.

This approach gives you the advantage of handling a wider character set than the MacRoman encoding in the pure Quartz text drawing can. However, it is slightly slower.

坏尐絯 2024-08-21 19:46:01

“将它们旋转到正常模式”到底是什么意思?

假设您只是指二维空间内的旋转,通常会使用变换矩阵。看看 CGAffineTransformMakeRotation

您可以将其应用到视图本身(通过设置 transform 属性),或者应用到您拥有的 CG 上下文。

What do you mean, exactly, by "rotate them to the normal mode"?

Assuming you just mean rotation within a 2D space, you would typically use a transform matrix. Have a look at CGAffineTransformMakeRotation.

You could apply it to the view itself (by setting the transform property), or to the CG context you have.

夜还是长夜 2024-08-21 19:46:01

好吧,这解决了我的问题

CGContextSetTextMatrix(context, CGAffineTransformMake(1.0,0.0, 0.0, -1.0, 0.0, 0.0));

Well this solved my problem

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