using Xui.Core.Canvas; using Xui.Core.Math2D; using Xui.Core.UI; using Xui.Core.UI.Input; using NFloat = System.Runtime.InteropServices.NFloat; public sealed class TextMeasureView : View { private bool large; protected override void RenderCore(IContext context) { context.SetFill(new Color(0xFAF7FFFF)); context.FillRect(Frame); context.SetFont(new Font(large ? 42 : 28, "Inter")); const string text = "Hello, Xui!"; var metrics = context.MeasureText(text); NFloat x = Frame.Center.X - metrics.Size.Width / 2; NFloat baseline = Frame.Center.Y; context.SetFill(new Color(0xEADCF5FF)); context.FillRect(new Rect(x, baseline - metrics.Font.FontBoundingBoxAscent, metrics.Size.Width, metrics.Font.Height)); context.SetStroke(new Color(0xD957A8FF)); context.LineWidth = 1; context.BeginPath(); context.MoveTo((Frame.X + 24, baseline)); context.LineTo((Frame.Right - 24, baseline)); context.Stroke(); context.TextAlign = TextAlign.Left; context.TextBaseline = TextBaseline.Alphabetic; context.SetFill(new Color(0x492571FF)); context.FillText(text, (x, baseline)); context.SetFont(new Font(17, "Inter")); context.TextAlign = TextAlign.Center; context.FillText("Pink line: alphabetic baseline", (Frame.Center.X, baseline + 72)); context.FillText("Tap to change the font size", (Frame.Center.X, Frame.Y + 56)); } public override void OnPointerEvent(ref PointerEventRef e, EventPhase phase) { if (phase == EventPhase.Bubble && e.Type == PointerEventType.Down) { large = !large; InvalidateRender(); } } }