Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply best practices for creating programmatic videos with Remotion and React
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
rules/measuring-text.md
1---2name: measuring-text3description: Measuring text dimensions, fitting text to containers, and checking overflow4metadata:5tags: measure, text, layout, dimensions, fitText, fillTextBox6---78# Measuring text in Remotion910## Prerequisites1112Install @remotion/layout-utils if it is not already installed:1314```bash15npx remotion add @remotion/layout-utils16```1718## Measuring text dimensions1920Use `measureText()` to calculate the width and height of text:2122```tsx23import { measureText } from "@remotion/layout-utils";2425const { width, height } = measureText({26text: "Hello World",27fontFamily: "Arial",28fontSize: 32,29fontWeight: "bold",30});31```3233Results are cached - duplicate calls return the cached result.3435## Fitting text to a width3637Use `fitText()` to find the optimal font size for a container:3839```tsx40import { fitText } from "@remotion/layout-utils";4142const { fontSize } = fitText({43text: "Hello World",44withinWidth: 600,45fontFamily: "Inter",46fontWeight: "bold",47});4849return (50<div51style={{52fontSize: Math.min(fontSize, 80), // Cap at 80px53fontFamily: "Inter",54fontWeight: "bold",55}}56>57Hello World58</div>59);60```6162## Checking text overflow6364Use `fillTextBox()` to check if text exceeds a box:6566```tsx67import { fillTextBox } from "@remotion/layout-utils";6869const box = fillTextBox({ maxBoxWidth: 400, maxLines: 3 });7071const words = ["Hello", "World", "This", "is", "a", "test"];72for (const word of words) {73const { exceedsBox } = box.add({74text: word + " ",75fontFamily: "Arial",76fontSize: 24,77});78if (exceedsBox) {79// Text would overflow, handle accordingly80break;81}82}83```8485## Best practices8687**Load fonts first:** Only call measurement functions after fonts are loaded.8889```tsx90import { loadFont } from "@remotion/google-fonts/Inter";9192const { fontFamily, waitUntilDone } = loadFont("normal", {93weights: ["400"],94subsets: ["latin"],95});9697waitUntilDone().then(() => {98// Now safe to measure99const { width } = measureText({100text: "Hello",101fontFamily,102fontSize: 32,103});104});105```106107**Use validateFontIsLoaded:** Catch font loading issues early:108109```tsx110measureText({111text: "Hello",112fontFamily: "MyCustomFont",113fontSize: 32,114validateFontIsLoaded: true, // Throws if font not loaded115});116```117118**Match font properties:** Use the same properties for measurement and rendering:119120```tsx121const fontStyle = {122fontFamily: "Inter",123fontSize: 32,124fontWeight: "bold" as const,125letterSpacing: "0.5px",126};127128const { width } = measureText({129text: "Hello",130...fontStyle,131});132133return <div style={fontStyle}>Hello</div>;134```135136**Avoid padding and border:** Use `outline` instead of `border` to prevent layout differences:137138```tsx139<div style={{ outline: "2px solid red" }}>Text</div>140```141