代码之家  ›  专栏  ›  技术社区  ›  Chilly Zhong

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

  •  3
  • Chilly Zhong  · 技术社区  · 16 年前

    我想用石英画一些文字。现在应用程序开始绘制 alt text http://www.freeimagehosting.net/uploads/71a84c9b49.png 但我想把它显示为“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
    
    4 回复  |  直到 13 年前
        1
  •  14
  •   Chintan Patel    16 年前

    在石英中,图像会颠倒,因为它的Y轴是颠倒的。

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

    下面是一些解决石英反演问题的代码:

    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

        2
  •  4
  •   Brad Larson    16 年前

    另一种处理方法是将Quartz文本绘制代码(从cgselectFont()替换为cgContextShowTextAtPoint())替换为

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

    其中,文本是nsstring。在iPhone上,-drawatpoint:withFont:自动翻转文本以说明ui视图的倒Y轴。

    这种方法使您能够处理比纯石英文本绘图中的Macroman编码更宽的字符集。不过,速度稍微慢一点。

        3
  •  0
  •   philsquared    16 年前

    你说的“旋转到正常模式”是什么意思?

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

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

        4
  •  0
  •   Lithu T.V    13 年前

    这个解决了我的问题

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