Using MonoTouch.CoreText to draw text fragments at specific coordinates

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Using MonoTouch.CoreText to draw text fragments at specific coordinates

guivho
Hi,

(Copy from stackoverflow post at
http://stackoverflow.com/questions/21309082/using-monotouch-coretext-to-draw-text-fragments-at-specific-coordinaates)

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/

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.

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);
                }
            }
        }
    }

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.

Any help, any one?

TIA,


Guido

--
But these pills can't be habit forming;
I've been taking them for years.
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Please help with 'Using MonoTouch.CoreText to draw text fragments'

guivho
Hi,

Just edited the subject to draw attention to my quest to get some help
with 'Using MonoTouch.CoreText to draw text fragments at specific
coordinates'.

TIA,

Guido.

Guido Van Hoecke <[hidden email]> writes:

> Hi,
>
> (Copy from stackoverflow post at
> http://stackoverflow.com/questions/21309082/using-monotouch-coretext-to-draw-text-fragments-at-specific-coordinaates)
>
> 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/
>
> 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.
>
> 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);
>                 }
>             }
>         }
>     }
>
> 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.
>
> Any help, any one?
>
> TIA,
>
>
> Guido
>
> --
> But these pills can't be habit forming;
> I've been taking them for years.
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Please help with 'Using MonoTouch.CoreText to draw text fragments'

Eric J. M. Smith

On 27-Jan-2014, at 13:57 , Guido Van Hoecke wrote:

>> 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.

Hi Guido,

I had a similar experience trying to label a graph using CoreText.  The first label was fine, but subsequent ones were drawn in the wrong place.  I thought that I would be safe by bracketing everything inside CGContext.SaveState/RestoreState, but that didn't work for me.  Like you, I seem to lack some basic understanding.

In the end, I gave up on CoreText, and rewrote my label-drawing code to use NSAttributedString.DrawString.  A much simpler API.

Hope that helps,

Eric Smith
Tarkvara Design Inc.
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Please help with 'Using MonoTouch.CoreText to draw text fragments'

guivho
Hi Eric,

Thanks for your reply.

"Eric J. M. Smith" <[hidden email]> writes:

> On 27-Jan-2014, at 13:57 , Guido Van Hoecke wrote:
>
>>> 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.
>
> I had a similar experience trying to label a graph using CoreText.
> The first label was fine, but subsequent ones were drawn in the wrong
> place.  I thought that I would be safe by bracketing everything inside
> CGContext.SaveState/RestoreState, but that didn't work for me.  Like
> you, I seem to lack some basic understanding.
>
> In the end, I gave up on CoreText, and rewrote my label-drawing code
> to use NSAttributedString.DrawString.  A much simpler API.

I need to draw text at specific places in a UI(Touch)View and I do not
see how NSAttributedString.DrawString could do that for me :(

So if there's anyone out there who could shed some light on how to use
CoreText to draw text at computed coordinates... please help!

TIA,


Guido

--
Look ere ye leap.
                -- John Heywood
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch
Reply | Threaded
Open this post in threaded view
|

Re: Please help with 'Using MonoTouch.CoreText to draw text fragments'

guivho
Hi Eric,

Indeed simple DrawString at PointF does what I need :)

Can't rememeber why I ever thought that I needed CoreText :(

Thank you so much!

Guido


On 28 January 2014 17:54, Eric J. M. Smith <[hidden email]> wrote:

>
> On 28-Jan-2014, at 03:49 , Guido Van Hoecke wrote:
>
>> I need to draw text at specific places in a UI(Touch)View and I do not
>> see how NSAttributedString.DrawString could do that for me :(
>
> You pass DrawString() either a PointF to draw at or a RectangleF to draw at a specific place in the current CGContext.
>
> I was a little surprised that Apple added drawing methods to their basic string classes (there's a bunch of NSString.DrawString methods too).  It seems like an odd design choice.  But I'm not complaining, the methods work.  And they're a whole lot simpler than using CoreText.
>
> Good luck,
>
> E.
>
_______________________________________________
MonoTouch mailing list
[hidden email]
http://lists.ximian.com/mailman/listinfo/monotouch