Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xamarin.ios MonoTouch:自定义图形文字始终绘制翻转文字_Xamarin.ios - Fatal编程技术网

Xamarin.ios MonoTouch:自定义图形文字始终绘制翻转文字

Xamarin.ios MonoTouch:自定义图形文字始终绘制翻转文字,xamarin.ios,Xamarin.ios,在UIView子类中,我覆盖了Draw子类来绘制一些自定义文本,但是文本始终被绘制为翻转文本 这是我正在使用的代码: class TextViewProblem : UIView { public override void Draw (RectangleF rect) { base.Draw (rect); CGContext g = UIGraphics.GetCurrentContext(); UIColor.White.SetF

在UIView子类中,我覆盖了Draw子类来绘制一些自定义文本,但是文本始终被绘制为翻转文本

这是我正在使用的代码:

class TextViewProblem : UIView
{
public override void Draw (RectangleF rect)
    {
        base.Draw (rect);

        CGContext g = UIGraphics.GetCurrentContext();

        UIColor.White.SetFill();

        g.SelectFont("Arial",16f,CGTextEncoding.MacRoman);

        UIColor.White.SetFill();
        g.SetTextDrawingMode(CGTextDrawingMode.Fill);
        g.ShowTextAtPoint(1,25,"Yiannis 123");



    }

}
这是这个代码的输出:

为什么文本被翻转了

我正在跑步: MonoDevelop 2.4.2 iPhone模拟器4.2 MonoTouch 3.2.6


您可以从以下链接下载一个项目来重现此问题:

由于CoreGraphics的坐标系与UIKit的坐标系不同,因此图像被翻转,您需要应用一个变换,包括围绕渲染翻转(缩放x=1,y=-1),然后按高度平移(x=0,y=height)


您可以通过将转换应用于图形上下文来完成此操作。

以下是有效的代码:

        CGContext g = UIGraphics.GetCurrentContext();
        UIColor.White.SetFill();
        g.ScaleCTM(1f,-1f);
        g.SelectFont("Arial",16f,CGTextEncoding.MacRoman);
        UIColor.White.SetFill();
        g.SetTextDrawingMode(CGTextDrawingMode.Fill);
        g.ShowTextAtPoint(1,-50,"Yiannis 123");
一旦将Y的比例设置为-1,现在坐标系就颠倒了。所以(0,0)是左上角,但左下角现在是(0,-480),而不是(0480)。因此请注意
ShowTextAtPoint
中的-50,该代码工作正常:

public override void Draw (System.Drawing.RectangleF rect)
    {
        base.Draw (rect);

        CGContext g = UIGraphics.GetCurrentContext();

        g.TranslateCTM(0,Bounds.Height);
        g.ScaleCTM(1f,-1f);
        g.DrawImage ..........
    }

提示是:首先是TranslateCTM,然后是ScaleCTM。

我尝试过这个方法,但没有成功:gctx.ScaleCTM(1,-1);gctx.TranslateCTM(0,此帧宽度);我还尝试了g.ScaleCTM(1,-1);g、 TranslateCTM(0,此帧高度);但它不起作用。我得到了一个黑色的UIView,首先转换,然后缩放。我发现在Draw覆盖中,我可以使用UIView的DrawString方法。这样可以很好地绘制文本。您已经设置了两次填充。
using (CGContext g = UIGraphics.GetCurrentContext()) {
            g.ScaleCTM (1f, -1f);
            g.TranslateCTM (0, -Bounds.Height);
               ....