Xamarin模拟器上的文本定位是错误的

时间:2016-07-29 21:43:46

标签: c# xamarin.ios

我正在尝试开发一些简单的例程来制作图表,而且我在iPhone上定位文本时遇到了麻烦。我正在使用在Macbook OS X(10.11.6)上运行的Xamarin Studio(6.0.1)的iPhone 6s模拟器。

我的C#图表例程测试是:

DrawText(1.0f, 10.0f, "LowerLeft", red, Horizontal.Right, Vertical.Above);
DrawText(1.0f, 320.0f, "UpperLeft", red, Horizontal.Right, Vertical.Below);

iPhone 6s模拟器屏幕如下所示:

enter image description here

我不明白为什么" UpperLeft"文本向右移动。

我的绘制功能是:

public override void Draw (CGRect rect) {
    base.Draw (rect);
    G.TestPattern();
}

我的G库是:

using System;
using System.Diagnostics;
using CoreGraphics;
using CoreText;
using Foundation;
using UIKit;

namespace TestPattern {
    public static class G {
        public enum Vertical {
            Above,
            Middle,
            Below
        };

        public enum Horizontal {
            Left,
            Center,
            Right
        };

        static CGColor red = UIColor.Red.CGColor;
        private static float canvasWidth, canvasHeight;

        public static void SetScreenSize(nfloat screenWidth, nfloat screenHeight) {
            canvasWidth  = (float) screenWidth;
            canvasHeight = (float) screenHeight;
        }

        public static nfloat LowerLeftOrigin(float yc) {
            // yc - y in Cartesian space
            // yd - y in Display coordinate space
            nfloat yd = canvasHeight - yc;
            return yd;
        }

        public static void DrawText(
            float x_,
            float y_,
            string text,
            CGColor color,
            Horizontal horizontal,
            Vertical vertical
        ){
            try {
                var context = UIGraphics.GetCurrentContext();
                context.SaveState();

                var attributes = new CTStringAttributes {
                    ForegroundColorFromContext = true,
                    Font = new CTFont("Arial", 12)
                };
                var attributedString = new NSAttributedString(text, attributes);
                var textWidth = (float) attributedString.Size.Width;
                var textHeight = (float) attributedString.Size.Height;
                Debug.WriteLine("text: " + text + ", textWidth: " + textWidth + ", textHeight: " + textHeight);

                var x = x_;
                var y = G.LowerLeftOrigin(y_);

                switch (horizontal) {
                    case Horizontal.Center:
                        x -= textWidth/2.0f;
                        break;
                    case Horizontal.Right:
                        break;
                    case Horizontal.Left:
                        x -= textWidth;
                        break;
                }
                switch (vertical) {
                    case Vertical.Above:
                        //y -= textHeight;
                        break;
                    case Vertical.Middle:
                        y -= textHeight/2.0f;
                        break;
                    case Vertical.Below:
                        y += textHeight;
                        break;
                }

                context.TranslateCTM(x, y);
                context.ScaleCTM(1, -1);
                context.SetFillColor(color);

                using (var textLine = new CTLine(attributedString)) {
                    textLine.Draw(context);
                }
                context.RestoreState();
            } catch (Exception ex) {
                Debug.WriteLine("DrawText Error: " + ex.Message);
            }
        }

        public static void TestPattern() {
            DrawText(1.0f, 10.0f, "LowerLeft", red, Horizontal.Right, Vertical.Above);
            DrawText(1.0f, 320.0f, "UpperLeft", red, Horizontal.Right, Vertical.Below);
        }
    }
}

如果我注释掉其中一个DrawText调用(任一个),那么字符串将被绘制在正确的位置。如果我同时绘制它们,则第二个调用将字符串向右移动。

非常感谢任何帮助。

查尔斯

0 个答案:

没有答案