public static List<string> GetWrappedText(CanvasContext2D ctx, string text, double width) { List<string> lines = new List<string>(); string[] words = text.Split(" "); string currentLine = ""; for (int i = 0; i < words.Length; i++) { if (!string.IsNullOrEmpty(words[i])) { if (currentLine == "" || ctx.MeasureText(currentLine + " " + words[i]).Width < width) { currentLine += " " + words[i]; } else { lines.Add(currentLine); currentLine = words[i]; } } } if (currentLine != "") { lines.Add(currentLine); } return lines; }
public override void Draw(CanvasContext2D canv) { if (!Visible) return; canv.Save(); if (!Focused) { CursorPosition = -1; DragPosition = -1; } DrawTicks++; can = canv; canv.StrokeStyle = ButtonBorderGrad; canv.FillStyle = Clicking ? Button1Grad : Button2Grad; canv.LineWidth = 2; CHelp.RoundRect(canv, TotalX, TotalY, Width, Height, 2, true, true); if (canv.Font != Font) canv.Font = Font; if (DragPosition != -1) { canv.FillStyle = "#598AFF"; var w1 = canv.MeasureText(Text.Substring(0, Math.Min(DragPosition, CursorPosition))).Width; var w2 = canv.MeasureText(Text.Substring(0, Math.Max(DragPosition, CursorPosition))).Width; canv.FillRect(TotalX + 8 + w1, TotalY + 3, w2 - w1, ( Height - 7 )); } canv.FillStyle = "#000000"; int hc; if (canv.Font.IndexOf("pt") != -1) hc = int.Parse(canv.Font.Substr(0, canv.Font.IndexOf("pt"))); else hc = int.Parse(canv.Font.Substr(0, canv.Font.IndexOf("px"))); canv.FillText(Text, TotalX + 8, TotalY + ( ( Height - hc ) / 2 ) + Height / 2); if (Focused && ( ( blinkTick++ % 35 ) == 0 )) blinked = !blinked; if (Focused && blinked) { canv.StrokeStyle = "#000000"; var w = canv.MeasureText(Text.Substring(0, CursorPosition)).Width; canv.BeginPath(); canv.MoveTo(TotalX + 8 + w, TotalY + 3); canv.LineTo(TotalX + 8 + w, TotalY + ( Height - 7 )); canv.LineWidth = 2; canv.Stroke(); } canv.Restore(); base.Draw(canv); }