Guides/5 min

measureText in practice

Width was the only number for years. You have a full em box now. Use it.

TextMetrics today includes width, actualBoundingBoxAscent/Descent/Left/Right, fontBoundingBoxAscent/Descent, and in some engines emHeightAscent/Descent. They answer different questions.

  • width — advance width, including side bearings. Use it to wrap and to align.
  • fontBoundingBox* — the face’s em box. Use it to stack lines so accents on one line do not collide with the next.
  • actualBoundingBox* — the ink of this specific string. Use it to crop, to draw a tight selection, to place a rule under a word.

Line height on canvas is not CSS line-height. Pick fontSize * 1.2 (or whatever your design is) and place each alphabetic baseline at originY + i * lineBox. The Metrics overlay in the studio draws baseline, em box, and ink box so you can see the difference on the face you actually chose.

Wrapping

There is no native wrap. Split on \n first (authors mean those), then greedy-fit words against maxWidth using the same width function you will draw with — including tracking. If you measure without tracking and draw with tracking, the last word on the line will clip.