internal void DrawItem(SKCanvas canvas) { if (Selected) { canvas.FillRectangle(Bounds, Theme.RibbonItemHighlightColor); } var image_size = LogicalToDeviceUnits(32); var image_area = new Rectangle(Bounds.Left, Bounds.Top, Bounds.Width, Bounds.Width); var image_bounds = DrawingExtensions.CenterSquare(image_area, image_size); image_bounds.Y = Bounds.Top + LogicalToDeviceUnits(3); if (Image != null) { canvas.DrawBitmap(Image, image_bounds); } if (!string.IsNullOrWhiteSpace(Text)) { var font_size = LogicalToDeviceUnits(Theme.RibbonItemFontSize); canvas.Save(); canvas.Clip(Bounds); var text_bounds = new Rectangle(Bounds.Left, image_bounds.Bottom + LogicalToDeviceUnits(3), Bounds.Width, Bounds.Bottom - image_bounds.Bottom - LogicalToDeviceUnits(3)); canvas.DrawText(Text, Theme.UIFont, font_size, text_bounds, Theme.DarkTextColor, ContentAlignment.MiddleCenter); canvas.Restore(); } }
/// <summary> /// Draws a string of text. /// </summary> public static void DrawText(this SKCanvas canvas, string text, SKTypeface font, int fontSize, Rectangle bounds, SKColor color, ContentAlignment alignment, int selectionStart = -1, int selectionEnd = -1, SKColor?selectionColor = null, int?maxLines = null, bool ellipsis = false) { if (string.IsNullOrWhiteSpace(text)) { return; } var tb = TextMeasurer.CreateTextBlock(text, font, fontSize, bounds.Size, TextMeasurer.GetTextAlign(alignment), color, maxLines, ellipsis); var location = bounds.Location; var vertical = TextMeasurer.GetVerticalAlign(alignment); if (vertical == SKTextAlign.Right) { location.Y = bounds.Bottom - (int)tb.MeasuredHeight; } else if (vertical == SKTextAlign.Center) { location.Y += (bounds.Height - (int)tb.MeasuredHeight) / 2; } var options = CreateOptions(); if (selectionStart >= 0 && selectionEnd >= 0 && selectionStart != selectionEnd) { options.Selection = new TextRange(selectionStart, selectionEnd); options.SelectionColor = selectionColor ?? SKColors.Blue; } canvas.Save(); canvas.Clip(bounds); tb.Paint(canvas, new SKPoint(location.X, location.Y), options); canvas.Restore(); }
//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(); }