2009-11-09 144 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); 
      } 

     } 
    }