Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.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
C# 使用MonoTouch.CoreText在特定坐标处绘制文本片段_C#_Ios_Xamarin.ios_Core Text - Fatal编程技术网

C# 使用MonoTouch.CoreText在特定坐标处绘制文本片段

C# 使用MonoTouch.CoreText在特定坐标处绘制文本片段,c#,ios,xamarin.ios,core-text,C#,Ios,Xamarin.ios,Core Text,我正试图掌握CoreText,在 作为一个测试,我想在 各自的立场。但是只有第一个(N)被画出来 以下是我的TextDrawingView.cs版本: using System; using System.Drawing; using MonoTouch.UIKit; using MonoTouch.Foundation; using MonoTouch.CoreText; using MonoTouch.CoreGraphics; namespace CoreTextDrawing {

我正试图掌握CoreText,在

作为一个测试,我想在 各自的立场。但是只有第一个(N)被画出来

以下是我的TextDrawingView.cs版本:

using System;
using System.Drawing;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using MonoTouch.CoreText;
using MonoTouch.CoreGraphics;

namespace CoreTextDrawing
{
    public class TextDrawingView : UIView
    {
        public TextDrawingView ()
        {
        }

        //based upon docs.xamarin.com/recipes/ios/graphics_and_drawing/\
        //    core_text/draw_unicode_text_with_coretext/

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

            var gctx = UIGraphics.GetCurrentContext ();
            gctx.SetFillColor (UIColor.Green.CGColor);

            DrawText ("N", Bounds.Width / 2, Bounds.Height / 4, gctx);
            DrawText ("W", Bounds.Width / 4, Bounds.Height / 2, gctx);
            DrawText ("E", Bounds.Width / 4 * 3, Bounds.Height / 2, gctx);
            DrawText ("S", Bounds.Width / 2, Bounds.Height / 4 * 3, gctx);
        }

        private void DrawText (string t, float x, float y, CGContext gctx)
        {
            gctx.TranslateCTM (x, y);
            gctx.ScaleCTM (1, -1);
            var attributedString = new NSAttributedString (t,
                                       new CTStringAttributes {
                    ForegroundColorFromContext = true,
                    Font = new CTFont ("Arial", 24)
                }); 

            using (var textLine = new CTLine (attributedString)) { 
                textLine.Draw (gctx);
            }
        }
    }
}
我不知道为什么只画N。4个绘图文本中的每一个 如果调用是唯一的调用,则可以正常工作

我似乎缺乏一些基本的理解

基本上我想在地图上的特定坐标处画一些字母 屏幕,但未能理解如何实现这一点

有人帮忙吗

蒂亚


Guido

您需要在DrawText方法的开头和结尾保存和恢复上下文状态

gctx.SaveState(); 
...
Transformations
DrawText
...
gctx.RestoreState();

谢谢你的评论。我用完全不同的方法修复了我原来的问题。如果我需要上述方法,我会记住你的建议。圭多