import Toybox.Graphics; import Toybox.Lang; import Toybox.WatchUi; // Text layout helpers. CIQ's drawText does not wrap, so callers split // a string by a separator and hand us an Array which we // render vertically centered around (cx, cy). module TextUtils { const LINE_SEPARATOR = "|"; function splitLines(text as String) as Array { var result = [] as Array; var current = ""; for (var i = 0; i < text.length(); i++) { var c = text.substring(i, i + 1); if (c.equals(LINE_SEPARATOR)) { result.add(current); current = ""; } else { current += c; } } result.add(current); return result; } function drawCentered(dc as Dc, lines as Array, cx as Number, cy as Number, font as Graphics.FontType) as Void { var lineHeight = dc.getFontHeight(font); var totalHeight = lines.size() * lineHeight; var startY = cy - totalHeight / 2 + lineHeight / 2; for (var j = 0; j < lines.size(); j++) { dc.drawText(cx, startY + j * lineHeight, font, lines[j], Graphics.TEXT_JUSTIFY_CENTER | Graphics.TEXT_JUSTIFY_VCENTER); } } // Convenience: load a string resource and split + draw in one call. function drawResourceCentered(dc as Dc, resourceId as ResourceId, cx as Number, cy as Number, font as Graphics.FontType) as Void { var text = WatchUi.loadResource(resourceId) as String; drawCentered(dc, splitLines(text), cx, cy, font); } }