2009-11-09 148 views
5

我想在视图中绘制一些文字,旋转90°。我对iPhone开发很陌生,并且在网络上发现了许多不同的解决方案。我已经尝试了一些,通常最终我的文字被剪辑。iPhone:绘制旋转文字?

这是怎么回事? I am绘制在一个相当小的空间(一个表格视图单元格),但必须有一个“正确”的方式来做到这一点...对吗?


编辑:下面是几个例子。我试图在左边的黑色条上显示文字“”。

  1. 第一次尝试,从RJShearman on the Apple Discussions

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSelectFont (context, "Helvetica-Bold", 16.0, kCGEncodingMacRoman); 
    CGContextSetTextDrawingMode (context, kCGTextFill); 
    CGContextSetRGBFillColor(context, 1.0, 0.0, 0.0, 1.0); 
    CGContextSetTextMatrix (context, CGAffineTransformRotate(CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f), M_PI/2)); 
    CGContextShowTextAtPoint (context, 21.0, 55.0, [_cell.number cStringUsingEncoding:NSUTF8StringEncoding], [_cell.number length]); 
    CGContextRestoreGState(context); 
    

    Attempt one. The one and part of the two are clipped out. http://dev.deeptechinc.com/sidney/share/iphonerotation/attempt1.png

  2. 第二次尝试,从zgombosi on iPhone Dev SDK。相同的结果(字体在这里略小,所以裁剪更少)。

    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGPoint point = CGPointMake(6.0, 50.0); 
    CGContextSaveGState(context); 
    CGContextTranslateCTM(context, point.x, point.y); 
    CGAffineTransform textTransform = CGAffineTransformMakeRotation(-1.57); 
    CGContextConcatCTM(context, textTransform); 
    CGContextTranslateCTM(context, -point.x, -point.y); 
    [[UIColor redColor] set]; 
    [_cell.number drawAtPoint:point withFont:[UIFont fontWithName:@"Helvetica-Bold" size:14.0]]; 
    CGContextRestoreGState(context); 
    

    Attempt two. There is almost identical clipping http://dev.deeptechinc.com/sidney/share/iphonerotation/attempt2.png

+0

确实有几种方法可以解决这个问题。提供一些关于你已经尝试了什么以及如何解决问题的细节(它是如何被删减的?)会让它更容易有效地回答。 – 2009-11-09 21:17:39

+0

提供了详细信息。 – s4y 2009-11-09 23:15:49

回答

7

事实证明,我的表单元格是总是无论行高如何都初始化为44px高,所以我的所有绘图都从单元格顶部剪下44px。

为了吸引更大的电池,有必要设置内容视图的autoresizingMask

cellContentView.autoresizingMask = UIViewAutoresizingFlexibleHeight; 

cellContentView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; 

...和drawRect被称为具有正确大小。从某种意义上说,这是有道理的,因为UITableViewCellinitWithStyle:reuseIdentifier:没有提及单元格的大小,只有表格视图实际上知道每行的大小,这取决于它自己的大小及其代表对tableView:heightForRowAtIndexPath:的响应。

我读Quartz 2D Programming Guide,直到图纸模型和功能开始有了意义,并提请我的旋转的文本代码变得简单明了:

CGContextRef context = UIGraphicsGetCurrentContext(); 
CGContextSaveGState(context); 
CGContextRotateCTM(context, -(M_PI/2)); 
[_cell.number drawAtPoint:CGPointMake(-57.0, 5.5) withFont:[UIFont fontWithName:@"Helvetica-Bold" size:16.0]]; 
CGContextRestoreGState(context); 

感谢您的提示,它看起来像我可以了,好了。

1

您了解的UITableViewDelegate方法heightForRowAtIndexPath吧?

这里是一个simple tutorial on various graphics level methods.假设你知道你的文本有多大,你应该能够适当地调整你的表视图行大小。

此外,我会检查以确保任何转换后的边界实际上符合您的期望。 (使用调试器或日志语句来验证这一点)。

+0

我知道'tableView:heightForRowAtIndexPath:',行本身很好。文本在变换过程中被截断,并在单元格中间截断。 – s4y 2009-11-09 21:32:17

+0

你能告诉我们你用来做转换的特定代码吗? – 2009-11-09 22:36:40

2

这是小费。我认为你在drawRect中做这个绘图。你为什么不在drawRect周围画一个框架来查看矩形的大小,如果这就是你剪裁的原因。

另一种方法是将文本放在UILabel中,然后在cellForRowAtIndexPath中旋转90度。

+2

mahboudz的建议可能会成为你抵抗力最小的路径。你可以用下面的方法旋转UILabel 90deg:[label setTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.0f))];你只需要根据标签的宽度来计算你的单元格高度。 -Matt – 2009-11-10 00:09:28

0

当我发现我需要将以下内容添加到我的文件顶部时,我喜欢Matt的方法。很简单。

#define degreesToRadian(x) (M_PI * (x)/180.0) 

mahboudz的建议可能是你的阻力最小的路径。你可以用下面的方法旋转UILabel 90deg:[label setTransform:CGAffineTransformMakeRotation(DegreesToRadians(-90.0f))];你只需要根据标签的宽度来计算你的单元格高度。 -Matt - 马特龙11月10日在0:09

3

用途: -

label.transform = CGAffineTransformMakeRotation(- 90.0f * M_PI/180.0f); 

哪里的标签是的UILabel的对象。

+0

谢谢,@Sorted!实际上,我没有使用'UILabel' - 我正在绘制自定义表格视图单元格。 – s4y 2011-10-10 13:58:15

1

什么@Sidnicious说些什么我收集通过了堆栈溢出,我想给一个使用例子 - 附加我的代码,以彻底划清尺子左侧屏幕侧,与旋转数:

ruler screenshot

RulerView : UIView 


    // simple testing for iPhones (check for device descriptions to get all iPhones + iPads) 
    - (float)getPPI 
    { 
     switch ((int)[UIScreen mainScreen].bounds.size.height) { 
      case 568: // iPhone 5* 
      case 667: // iPhone 6 
       return 163.0; 
       break; 

      case 736: // iPhone 6+ 
       return 154.0; 
       break; 

      default: 
       return -1.0; 
       break; 
     } 
    } 

    - (void)drawRect:(CGRect)rect 
    { 
     [[UIColor blackColor] setFill]; 


     float ppi = [self getPPI]; 

     if (ppi == -1.0) // unable to draw, maybe an ipad. 
      return; 

     float linesDist = ppi/25.4; // ppi/mm per inch (regular size iPad would be 132.0, iPhone6+ 154.0) 

     float linesWidthShort = 15.0; 
     float linesWidthMid = 20.0; 
     float linesWidthLong = 25.0; 

     for (float i = 0, c = 0; i <= self.bounds.size.height; i = i + linesDist, c = c +1.0) 
     { 
      bool isMid = (int)c % 5 == 0; 
      bool isLong = (int)c % 10 == 0; 

      float linesWidth = isLong ? linesWidthLong : isMid ? linesWidthMid : linesWidthShort; 
      UIRectFillUsingBlendMode((CGRect){0, i, linesWidth, .5} , kCGBlendModeNormal); 



      /* FONT: Numbers without rotation (yes, is short) 
      if (isLong && i > 0 && (int)c % 10 == 0) 
       [[NSString stringWithFormat:@"%d", (int)(c/10)] drawAtPoint:(CGPoint){linesWidthLong +2, i -5} withAttributes:@{ 
                                   NSFontAttributeName: [UIFont systemFontOfSize:9], 
                                   NSBaselineOffsetAttributeName: [NSNumber numberWithFloat:1.0] 
                                   }]; 
      */ 


      // FONT: Numbers with rotation (yes, requires more effort) 
      if (isLong && i > 0 && (int)c % 10 == 0) 
      { 
       NSString *str = [NSString stringWithFormat:@"%d", (int)(c/10)]; 
       NSDictionary *attrs = @{ 
             NSFontAttributeName: [UIFont systemFontOfSize:9], 
             NSBaselineOffsetAttributeName: [NSNumber numberWithFloat:0.0] 
             }; 
       CGSize textSize = [str sizeWithAttributes:attrs]; 


       CGContextRef context = UIGraphicsGetCurrentContext(); 
       CGContextSaveGState(context); 

       CGContextRotateCTM(context, +(M_PI/2)); 
       [str drawAtPoint:(CGPoint){i - (textSize.width/2), -(linesWidthLong + textSize.height +2)} withAttributes:attrs]; 

       CGContextRestoreGState(context); 
      } 

     } 
    }