Measure text
LynxTextMeasure tells you how wide a string will be, and where it will wrap, before you render it. Use it to:
- check whether a label fits, and shorten it or pick a smaller size if it doesn't
- size a container to its text, such as a chip, a badge or a tooltip
- count the lines a paragraph takes at a given width, or get the text of each line
Measure a string
src/app/chip.ts
measure(text, options)is synchronous. It returns{ width }right away, so you can use it in acomputed().fontSizeis required, and it takespxonly, as a string:'14px'.widthis in pixels.- Match your CSS. Pass the same
fontSizeyour<text>uses, or the numbers won't match what's drawn.
Find where text wraps
Add maxWidth and maxLine to get the text split into lines:
contentholds the text of each line, up tomaxLinelines.maxWidthtakespxonly.- With
maxLine: 1, there's nocontent. You get onlywidth.
Fonts it can measure
Measurement uses the platform's built-in fonts only.
- Custom fonts aren't used. A font you load with
@font-faceorLynxFont(see Custom fonts) is measured as the default font, so the result is wrong for it. fontFamilypicks a built-in font by name, such as'PingFang SC'on iOS. Leave it out for the default font.- Only the options above count.
font-weightandletter-spacingaren't supported, so bold text measures like regular text.
In the web preview and in tests
measure() needs Lynx's text engine. Outside a device or Lynx Explorer, it throws:
- In the web preview, it throws
lynx.getTextInfo is not a function. - In unit tests, it throws too. Mock
LynxTextMeasurewith a provider.
If a component must also render in the web preview, catch the error and fall back to an estimate:
