measureText()
Part of the @remotion/layout-utils
package.
Calculates the width and height of specified text to be used for layout calculations. Only works in the browser, not in Node.js or Bun.
API
This function has a cache. If there are two duplicate arguments inputs, the second one will return the first result without repeating the calculation.
The function takes the following options:
text
Required string
Any string.
fontFamily
Required string
Same as CSS style font-family
fontSize
Required number
Same as CSS style font-size
. Only numbers allowed, unit px
fontWeight
string
Same as CSS style font-weight
letterSpacing
string
Same as CSS style font-spacing
.
Return value
An object with height
and width
Example
tsx
import {measureText } from "@remotion/layout-utils";consttext = "remotion";constfontFamily = "Arial";constfontWeight = "500";constfontSize = 12;constletterSpacing = "1px";measureText ({text ,fontFamily ,fontWeight ,fontSize ,letterSpacing ,}); // { height: 14, width: 20 }
tsx
import {measureText } from "@remotion/layout-utils";consttext = "remotion";constfontFamily = "Arial";constfontWeight = "500";constfontSize = 12;constletterSpacing = "1px";measureText ({text ,fontFamily ,fontWeight ,fontSize ,letterSpacing ,}); // { height: 14, width: 20 }