使用 MonoTouch.CoreText 在特定坐标处绘制文本片段 [英] Using MonoTouch.CoreText to draw text fragments at specific coordinaates

查看:38
本文介绍了使用 MonoTouch.CoreText 在特定坐标处绘制文本片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过示例应用程序了解 CoreTexthttp://docs.xamarin.com/recipes/ios/graphics_and_drawing/core_text/draw_unicode_text_with_coretext/

I am trying to get a grasp of CoreText, working from the example app at http://docs.xamarin.com/recipes/ios/graphics_and_drawing/core_text/draw_unicode_text_with_coretext/

作为测试,我想在视图中的视图上写入N"、E"、S"和W"各自的职位.但只有第一个('N')被绘制.

As a test I want to write to write 'N', 'E', 'S' and 'W' on the view in the respective positions. But only the first one (the 'N') gets drawn.

这是我的 TextDrawingView.cs 版本:

Here's my version of 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中的每一个如果它们是唯一的调用,则调用工作正常.

I have no idea why only the 'N' gets drawn. Each of the 4 DrawText invocations work fine if they are the only invocation.

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

I seem to lack some basic understanding.

基本上我想在特定坐标上绘制一些字母屏幕,但未能理解如何实现这一点.

Basically I want to draw some letters at specific coordinates on the screen, but failed to understand how to achieve this.

有人帮忙吗?

TIA,

圭多

推荐答案

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

You need to save and restore the context state in the beginning and end of your DrawText method.

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

这篇关于使用 MonoTouch.CoreText 在特定坐标处绘制文本片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆