2012-09-17 3 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(); 
} 

그리고 텍스트를 그릴 수있는 코드를 (나는 모든 플로트와 회전을 int로 교체 한주의하고) 당신의 회전을 부정 할 수 있습니다 : 된 점에 대해

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); 

건배 :

여기에 내가 내 테스트에 사용 된 코드입니다.

관련 문제