コード例 #1
0
        //public static void DrawCharacters (this SKCanvas canvas, string text, SKTypeface font, int fontsize, int x, int y, SKColor color)
        //{
        //    var emoji = StringUtilities.GetUnicodeCharacterCode ("🚀", SKTextEncoding.Utf32);
        //    font = SKFontManager.Default.MatchCharacter ('c');
        //    font.
        //    using (var paint = CreateTextPaint (font, fontsize, color)) {
        //        var ranges = TextMeasurer.MeasureCharacters (text, font, fontsize, x, y);
        //        var x_float = (float)x;
        //       // if (SKTypeface.Default.)
        //            canvas.DrawText (text, x_float, y, paint);
        //        //for (var i = 0; i < text.Length - 1; i++) {
        //        //    x_float = ranges[i].X;
        //        //}
        //    }
        //}

        public static void DrawText(this SKCanvas canvas, string text, SKTypeface font, int fontsize, Rectangle bounds, SKColor color, ContentAlignment alignment)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            using var paint = CreateTextPaint(font, fontsize, color);

            var font_height = new SKRect();

            paint.MeasureText(text, ref font_height);

            // See how many lines we can fit if needed
            var line_count = bounds.Height / font_height.Height;

            var vertical_align = GetVerticalAlign(alignment);
            var y = bounds.Top + (int)font_height.Height;

            if (vertical_align == SKTextAlign.Center)
            {
                var center_bounds = bounds.Top + (bounds.Height / 2);
                var text_center   = (font_height.Top + font_height.Bottom) / 2;

                y = (int)(center_bounds - text_center);
            }
            else if (vertical_align == SKTextAlign.Right)
            {
                y = bounds.Bottom - (int)font_height.Bottom - 1;
            }

            canvas.Save();
            canvas.Clip(bounds);

            bounds.Y = y;
            canvas.DrawTextLine(text, bounds, paint, alignment);

            canvas.Restore();
        }