C# 使用MonoTouch.CoreText在特定坐标处绘制文本片段
我正试图掌握CoreText,在 作为一个测试,我想在 各自的立场。但是只有第一个(N)被画出来 以下是我的TextDrawingView.cs版本: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 {
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();
谢谢你的评论。我用完全不同的方法修复了我原来的问题。如果我需要上述方法,我会记住你的建议。圭多