/// <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 Size GetPreferredSize(Size proposedSize) { var padding = Parent?.LogicalToDeviceUnits(Padding.Horizontal) ?? Padding.Horizontal; var font_size = Parent?.LogicalToDeviceUnits(Theme.FontSize) ?? Theme.FontSize; var text_size = (int)Math.Round(TextMeasurer.MeasureText(Text, Theme.UIFont, font_size)); return(new Size(text_size + padding, Bounds.Height)); }
public virtual Size GetPreferredSize(Size proposedSize) { var owner = OwnerControl; if (owner is Menu menu) { var padding = menu.LogicalToDeviceUnits(Padding.Horizontal); var font_size = menu.LogicalToDeviceUnits(Theme.FontSize); var text_size = (int)Math.Round(TextMeasurer.MeasureText(Text, Theme.UIFont, font_size).Width); return(new Size(text_size + padding, Bounds.Height)); } if (owner is ToolBar bar) { var width = bar.LogicalToDeviceUnits(Padding.Horizontal); var font_size = bar.LogicalToDeviceUnits(Theme.FontSize); width += (int)Math.Round(TextMeasurer.MeasureText(Text, Theme.UIFont, font_size).Width); if (!(Image is null)) { width += bar.LogicalToDeviceUnits(20); } if (HasItems) { width += bar.LogicalToDeviceUnits(14); } return(new Size(width, Bounds.Height)); } if (owner is MenuDropDown dropdown) { var padding = dropdown.LogicalToDeviceUnits(Padding); var font_size = dropdown.LogicalToDeviceUnits(Theme.FontSize); var text_size = TextMeasurer.MeasureText(Text, Theme.UIFont, font_size); return(new Size((int)Math.Round(text_size.Width, 0, MidpointRounding.AwayFromZero) + padding.Horizontal + dropdown.LogicalToDeviceUnits(70), (int)Math.Round(text_size.Height, 0, MidpointRounding.AwayFromZero) + dropdown.LogicalToDeviceUnits(8))); } return(proposedSize); }