Xamarin.ios 尝试使用CGAffineTransform绘制旋转文本,并使旋转出现在错误的位置

Xamarin.ios 尝试使用CGAffineTransform绘制旋转文本,并使旋转出现在错误的位置,xamarin.ios,cgaffinetransform,drawstring,Xamarin.ios,Cgaffinetransform,Drawstring,我试图在特定位置使用CGAffineTransform.MakeRotation方法绘制一些旋转文本。我也使用了TranslateCTM,但一定是有问题,因为旋转的文本没有对齐,并且在正确的x,y位置出现,这是我使用的简单代码,有人知道问题出在哪里吗 public override void Draw (RectangleF rect) { DrawTextRotated("Hello1",10,100,30); DrawTextRotat

我试图在特定位置使用CGAffineTransform.MakeRotation方法绘制一些旋转文本。我也使用了TranslateCTM,但一定是有问题,因为旋转的文本没有对齐,并且在正确的x,y位置出现,这是我使用的简单代码,有人知道问题出在哪里吗

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

谢谢

这里有一些代码,可以绘制文本左上角正确旋转的文本。目前,我不考虑你对文本对齐的使用

首先,一种实用方法,用于在预期文本显示的位置绘制标记:

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旋转替换为float,您可能需要否定旋转):

围绕点旋转需要先将点平移到原点,然后旋转,再旋转回原点
CGContext.TextMatrix
NSString.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);

干杯。

这里有一些代码,可以画出在文本左上角正确旋转的文本。目前,我不考虑你对文本对齐的使用

首先,一种实用方法,用于在预期文本显示的位置绘制标记:

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旋转替换为float,您可能需要否定旋转):

围绕点旋转需要先将点平移到原点,然后旋转,再旋转回原点
CGContext.TextMatrix
NSString.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);
干杯