使用MonoTouch.CoreText在特定的坐标上绘制文本片段

我正在尝试获取CoreText的帮助,从http://docs.xamarin.com/recipes/ios/graphics_and_drawing/core_text/draw_unicode_text_with_coretext/

作为一个testing,我想写在“N”,“E”,“S”和“W”的视图在各自的立场。 但是只有第一个('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个DrawText调用中的每一个都可以正常工作。

我似乎缺乏一些基本的了解。

基本上我想画在屏幕上的特定坐标的一些字母,但没有明白如何实现这一点。

任何帮助,任何一个?

TIA,

圭多

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

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