LineHeightStyle.Mode


Determines how line height is calculated across lines in a paragraph.

Fixed, Minimum, and Tight measure font metrics on the first line only and apply that single calculated height to every line in the paragraph. If subsequent lines are taller than the first line (such as lines with larger font sizes or taller scripts), they will still use the first line's height and may clip.

Text with mixed font sizes or mixed scripts should use PerLine, which measures each line independently.

Summary

Public companion properties

LineHeightStyle.Mode

Sets the same line height for each line, calculated from the font metrics of the first line only.

Cmn
LineHeightStyle.Mode

Sets the same line height for each line, calculated from the font metrics of the first line only.

Cmn
LineHeightStyle.Mode

Calculates line height independently for each line.

Cmn
LineHeightStyle.Mode

Sets the requested line height on every line based on the first line only, without adding top or bottom padding.

Cmn

Public functions

open String
Cmn

Public companion properties

Fixed

val FixedLineHeightStyle.Mode

Sets the same line height for each line, calculated from the font metrics of the first line only.

If the requested line height is smaller than the first line's font metrics, adds padding outside the first line top and last line bottom to prevent clipping at the paragraph boundaries. Middle lines are not padded and will clip if they overflow.

If later lines are taller than the first line, they do not expand and may clip.

This is the default mode in LineHeightStyle.Default.

Minimum

val MinimumLineHeightStyle.Mode

Sets the same line height for each line, calculated from the font metrics of the first line only.

If the requested line height is smaller than the first line's font metrics, uses the first line's natural height for all lines instead of shrinking them. If the requested line height is larger, behaves the same as Fixed.

If later lines are taller than the first line, they do not expand and may clip.

PerLine

val PerLineLineHeightStyle.Mode

Calculates line height independently for each line.

Unlike Fixed, Minimum, and Tight (which only measure the first line), PerLine measures every line. If a line's natural font height exceeds the requested line height, that line expands to its natural height.

Use PerLine when text contains mixed font sizes or mixed scripts so that tall lines do not clip and short lines do not leave extra gaps:

Mode.Fixed / Mode.Minimum (Only measures Line 1; Line 2 may clip):
+----------------------------------------------+
|
Line 1 (Small font, measures line height) |
+----------------------------------------------+
|
Line 2 (TALL SCRIPT - clips or overlaps!) |
+----------------------------------------------+
|
Line 3 (Small font) |
+----------------------------------------------+


Mode.PerLine (Measures each line independently):
+----------------------------------------------+
|
Line 1 (Small font, compact line height) |
+----------------------------------------------+
|
Line 2 (TALL SCRIPT - expands to fit) |
+----------------------------------------------+
|
Line 3 (Small font, compact line height) |
+----------------------------------------------+
import androidx.compose.material3.Text
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.style.LineHeightStyle
import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.sp

val text = buildAnnotatedString {
    append("This is a line with small text.\n")
    withStyle(SpanStyle(fontSize = 40.sp)) { append("TALL TEXT HERE\n") }
    append("Another line with small text.")
}
Text(
    text = text,
    style =
        TextStyle(
            fontSize = 16.sp,
            lineHeight = 24.sp,
            lineHeightStyle =
                LineHeightStyle(
                    alignment = LineHeightStyle.Alignment.Proportional,
                    trim = LineHeightStyle.Trim.None,
                    mode = LineHeightStyle.Mode.PerLine,
                ),
        ),
)

Tight

val TightLineHeightStyle.Mode

Sets the requested line height on every line based on the first line only, without adding top or bottom padding.

If the requested line height is smaller than the font metrics of any line, that line will clip.

Public functions

toString

open fun toString(): String