Example #1
0
        // TODO using Core Text here is going to be a huge problem because we have to
        // change the coordinate system for every call to DrawString rather than changing
        // it once for all the calls.  Not even sure we have enough info (the height of
        // the coord system or UIView) to do the transform?

        // TODO if we know the text is one-line, we can skip the big transform and just use
        // the textmatrix thing.

        public void DrawString(string s,
                               float box_x,
                               float box_y,
                               float box_width,
                               float box_height,
                               Xamarin.Forms.LineBreakMode lineBreak,
                               Xamarin.Forms.TextAlignment horizontal_align,
                               Xamarin.Forms.TextAlignment vertical_align
                               )
        {
            _c.SaveState();
            // TODO _c.SetTextDrawingMode (CGTextDrawingMode.Fill);

            // Core Text uses a different coordinate system.

            _c.TranslateCTM(0, _overall_height);
            _c.ScaleCTM(1, -1);

            var attributedString = new NSAttributedString(s, _ct_attr);

            float text_width;
            float text_height;

            if (
                (horizontal_align != TextAlignment.Start) ||
                (vertical_align != TextAlignment.Start))
            {
                // not all of the alignments need the bounding rect.  don't
                // calculate it if not needed.

                var sizeAttr = attributedString.GetBoundingRect(
                    new SizeF(
                        (float)box_width,
                        int.MaxValue),
                    NSStringDrawingOptions.UsesLineFragmentOrigin | NSStringDrawingOptions.UsesFontLeading,
                    null
                    );

                text_width = sizeAttr.Width;
                //text_height = sizeAttr.Height;
                //text_height = sizeAttr.Height + uif.Descender; // descender is negative
                text_height = _font_ui.CapHeight;
            }
            else
            {
                text_width  = 0;
                text_height = 0;
            }

            //Console.WriteLine ("width: {0}    height: {1}", text_width, text_height);

            float x;

            switch (horizontal_align)
            {
            case Xamarin.Forms.TextAlignment.End:
                x = (box_x + box_width) - text_width;
                break;

            case Xamarin.Forms.TextAlignment.Center:
                x = box_x + (box_width - text_width) / 2;
                break;

            case Xamarin.Forms.TextAlignment.Start:
            default:
                x = box_x;
                break;
            }

            float y;

            switch (vertical_align)
            {
            case Xamarin.Forms.TextAlignment.End:
                y = box_y + text_height;
                break;

            case Xamarin.Forms.TextAlignment.Center:
                y = (box_y + box_height) - (box_height - text_height) / 2;
                break;

            case Xamarin.Forms.TextAlignment.Start:
            default:
                y = (box_y + box_height);
                break;
            }

            _c.TextPosition = new PointF((float)x, (float)(_overall_height - y));

            // I think that by using CTLine() below, we are preventing multi-line text.  :-(

            using (var textLine = new CTLine(attributedString)) {
                textLine.Draw(_c);
            }

            //gctx.StrokeRect (new RectangleF(x, height - y, text_width, text_height));

            _c.RestoreState();
        }
Example #2
0
        public void DrawString(string s,
                               float box_x,
                               float box_y,
                               float box_width,
                               float box_height,
                               Xamarin.Forms.LineBreakMode lineBreak,
                               Xamarin.Forms.TextAlignment horizontal_align,
                               Xamarin.Forms.TextAlignment vertical_align
                               )
        {
            // TODO
            if (string.IsNullOrWhiteSpace(s))
            {
                return;
            }

            //SetTextAlign()

            float text_width;
            float text_height;

            if (
                (horizontal_align != TextAlignment.Start) ||
                (vertical_align != TextAlignment.Start))
            {
                // not all of the alignments need the bounding rect.  don't
                // calculate it if not needed.

                text_width = _paints.Text.MeasureText(s);

                text_height = -(TextFontMetrics.Ascent);
            }
            else
            {
                text_width  = 0;
                text_height = 0;
            }

            //Console.WriteLine ("width: {0}    height: {1}", text_width, text_height);

            float x;

            switch (horizontal_align)
            {
            case Xamarin.Forms.TextAlignment.End:
                x = (box_x + box_width) - text_width;
                break;

            case Xamarin.Forms.TextAlignment.Center:
                x = box_x + (box_width - text_width) / 2;
                break;

            case Xamarin.Forms.TextAlignment.Start:
            default:
                x = box_x;
                break;
            }

            float y;

            switch (vertical_align)
            {
            case Xamarin.Forms.TextAlignment.End:
                y = box_y + text_height;
                break;

            case Xamarin.Forms.TextAlignment.Center:
                y = (box_y + box_height) - (box_height - text_height) / 2;
                break;

            case Xamarin.Forms.TextAlignment.Start:
            default:
                y = (box_y + box_height);
                break;
            }

            _c.DrawText(s, x, y, _paints.Text);
        }