1

特定の場所で 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();
    }

ありがとう !

4

1 に答える 1

1

テキストの左上隅を中心に適切に回転されたテキストを描画するコードを次に示します。現時点では、テキストの配置の使用を無視しています。

まず、テキストが表示されると予想される場所にマーカーを描画するためのユーティリティ メソッド:

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 に置き換えたことに注意してください。回転を無効にすることもできます):

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

乾杯。

于 2012-09-20T17:57:21.903 に答える