2012-09-17 24 views
0

我試圖通過在特定位置使用CGAffineTransform.MakeRotation方法來繪製一些旋轉的文本。我也使用了TranslateCTM,但是一些東西一定是錯的,因爲旋轉文本不會對齊,並且它們應該出現在正確的x,y位置,這裏是我使用的簡單代碼,任何人都知道問題在哪裏? :試圖用CGAffineTransform和MakeRotation繪製旋轉文本出現在錯誤的位置

 public override void Draw (RectangleF rect) 
    { 
     DrawTextRotated("Hello1",10,100,30); 
     DrawTextRotated("Hello2",50,100,60); 
     SetNeedsDisplay();   
    }  

    static public float DegreesToRadians(float x) 
    {  
     return (float) (Math.PI * x/180.0); 
    } 

    public void DrawTextRotated(string text,int x, int y, int rotDegree) 
    { 
     CGContext c = UIGraphics.GetCurrentContext(); 
     c.SaveState(); 

     c.TextMatrix = CGAffineTransform.MakeRotation((float)DegreesToRadians((float)(-rotDegree)));       
     c.ConcatCTM(c.TextMatrix); 

     float xxx = ((float)Math.Sin(DegreesToRadians((float)rotDegree))*y); 
     float yyy = ((float)Math.Sin(DegreesToRadians((float)rotDegree))*x); 

     // Move the context back into the view 
     c.TranslateCTM(-xxx,yyy); 

     c.SetTextDrawingMode(CGTextDrawingMode.Fill); 
     c.SetShouldSmoothFonts(true); 

     MonoTouch.Foundation.NSString str = new MonoTouch.Foundation.NSString(text);    
     SizeF strSize = new SizeF(); 
     strSize = str.StringSize(UIFont.SystemFontOfSize(12));   

     RectangleF tmpR = new RectangleF(x,y,strSize.Width,strSize.Height); 

     str.DrawString(tmpR,UIFont.SystemFontOfSize(12),UILineBreakMode.WordWrap,UITextAlignment.Right); 
     c.RestoreState(); 
    } 

謝謝!

回答

0

下面是一些代碼,它將繪製文本正確旋轉的文本關於文本的左上角。目前,我無視您使用文本對齊方式。

首先,實用的方法來畫我們預期的文本顯示標記:

public void DrawMarker(float x, float y) 
{ 
    float SZ = 20; 

    CGContext c = UIGraphics.GetCurrentContext(); 

    c.BeginPath(); 
    c.AddLines(new [] { new PointF(x-SZ,y), new PointF(x+SZ,y) }); 
    c.AddLines(new [] { new PointF(x,y-SZ), new PointF(x,y+SZ) }); 
    c.StrokePath(); 
} 

並繪製文本的代碼(注意我已經更換了所有詮釋旋轉,帶漂浮,而你可能要否定你的旋轉):

public void DrawTextRotated(string text, float x, float y, float rotDegree) 
{ 
    CGContext c = UIGraphics.GetCurrentContext(); 
    c.SaveState(); 

    DrawMarker(x,y); 

    // Proper rotation about a point 
    var m = CGAffineTransform.MakeTranslation(-x,-y); 
    m.Multiply(CGAffineTransform.MakeRotation(DegreesToRadians(rotDegree))); 
    m.Multiply(CGAffineTransform.MakeTranslation(x,y)); 
    c.ConcatCTM(m); 

    // Draws text UNDER the point 
    // "This point represents the top-left corner of the string’s bounding box." 
    //http://developer.apple.com/library/ios/#documentation/UIKit/Reference/NSString_UIKit_Additions/Reference/Reference.html 
    NSString ns = new NSString(text); 
    UIFont font = UIFont.SystemFontOfSize(12); 
    SizeF sz = ns.StringSize(font); 
    RectangleF rect = new RectangleF(x,y,sz.Width,sz.Height); 
    ns.DrawString(rect, font); 

    c.RestoreState(); 
} 

繞點要求至原點,然後旋轉平移,依次輪換回到原點。 CGContext.TextMatrixNSString.DrawString沒有影響,因此您只能使用ConcatCTM

對齊和換行模式沒有任何影響。由於您使用的是NSString.StringSize,因此邊界矩形適合整個文本,與左邊和右邊緊密貼合。如果使邊界矩形的寬度更寬並使用UITextAlignment.Right,則會得到正確的右對齊,但文本仍將圍繞整個邊界矩形的左上角旋轉。這不是,我猜測,你在期待什麼。

如果您希望文字圍繞右上角旋轉,請告訴我們,我會相應地調整代碼。

這是我在我的測試中使用的代碼:

DrawTextRotated("Hello 0",100, 50, 0); 
DrawTextRotated("Hello 30",100,100,30); 
DrawTextRotated("Hello 60",100,150,60); 
DrawTextRotated("Hello 90",100,200,90); 

乾杯。