Text wrapping

Loading Ebi…
let sample = """
First   line.
Second line with several words.
AnExtraordinarilyLongWord
""".trim()

mut mode: "normal" | "pre-wrap" | "nowrap" | "pre-line" = "normal"
mut breaking: "normal" | "anywhere" = "normal"
mut panel_width = 300pt
let heading = <Text font_size={20pt} fill="blue2" position={[0pt,-90pt]}>{mode} · {breaking}</Text>
let panel = <Box width={panel_width} height={145pt} p-16 rounded-lg bg-card_bg_dark
    white_space={mode} overflow_wrap={breaking} overflow-x-hidden
    x_align={Align.Center} y_align={Align.Center}>
    <Text font_size={18pt} text={sample} />
</Box>
panel
heading

let resize = <Animation flow="sequence">
    {animate(panel_width, to=185pt, duration=1.4s, ease="smootherstep")}
    {animate(panel_width, to=300pt, duration=1.4s, ease="smootherstep")}
</Animation>

<Animation>
    {resize.repeat(3)}
    <Animation flow="sequence">
        {wait(800ms)}
        {animate(mode, to="pre-wrap", duration=0s)}
        {wait(1.6s)}
        {animate(breaking, to="anywhere", duration=0s)}
        {wait(1.2s)}
        {animate(mode, to="nowrap", duration=0s)}
        {wait(1.3s)}
        {animate(mode, to="pre-line", duration=0s)}
        {wait(1.5s)}
        {animate(mode, to="normal", duration=0s)}
        {animate(breaking, to="normal", duration=0s)}
    </Animation>
</Animation>

The panel keeps narrowing and widening as the wrapping rules change. Watch the spaces after First, the line break before Second, and where the long word breaks.

Triple quotes keep line breaks and spaces. Call .trim() to remove whitespace from both ends of the string. Use r""" when you want braces treated as literal text.

Choose a wrapping rule

Text starts with whitespace-normal. Apply a utility to the text or its parent Box, or bind white_space={mode} to change the rule during an animation.

UtilitySpaces and tabsExplicit line breaksWraps at the box edge
whitespace-normalCollapseCollapseYes
whitespace-pre-wrapKeepKeepYes
whitespace-pre-lineCollapseKeepYes
whitespace-nowrapCollapseCollapseNo
whitespace-preKeepKeepNo

Use wrap-anywhere or wrap-break-word to break a long word that would overflow a fixed width. To clip the overflow, use overflow-x-hidden. Code defaults to whitespace-pre and keeps source line numbers when a line wraps.

Choose a font

Set font_size={24pt} and font={Font(["sans-serif"])} to select a font family and size.

Set font_weight={650} for heavier text. Weight ranges from 1 to 1000 and defaults to 400. Variable fonts can use weights between their named styles.