Build apps faster with our new App builder! Check it out →

paddingFromBaseline

Common

Modifier in Compose Foundation Layout

A [Modifier] that positions the content in a layout such that the distance from the top of the layout to the [baseline of the first line of text in the content][FirstBaseline] is [top], and the distance from the [baseline of the last line of text in the content][LastBaseline] to the bottom of the layout is [bottom].

When the modified layout is min height constrained and the padded layout is smaller than the constraint, the modified layout will satisfy the min constraint and the content will be positioned to satisfy the [top] requirement if specified, or the [bottom] requirement otherwise.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.foundation:foundation-layout:1.8.0-alpha04")
}

Overloads

@Stable
fun Modifier.paddingFromBaseline(top: Dp = Dp.Unspecified, bottom: Dp = Dp.Unspecified)
@Stable
fun Modifier.paddingFromBaseline(
    top: TextUnit = TextUnit.Unspecified,
    bottom: TextUnit = TextUnit.Unspecified
)

Code Examples

PaddingFromBaselineSampleDp

@Composable
fun PaddingFromBaselineSampleDp() {
    // We want to have 30.dp distance from the top of the layout box to the baseline of the
    // first line of text, and a 40.dp distance from the bottom of the layout box to the baseline
    // of the last line of text. Note it is good practice to specify these distances in sp for font
    // scaling, which can be done with the other overload.
    val distanceToFirstBaseline = 30.dp
    val distanceFromLastBaseline = 40.dp
    Text(
        text = "This line has the first baseline.\nThis line has the last baseline.",
        modifier = Modifier.paddingFromBaseline(distanceToFirstBaseline, distanceFromLastBaseline)
    )
}

PaddingFromBaselineSampleTextUnit

@Composable
fun PaddingFromBaselineSampleTextUnit() {
    // We want to have 30.sp distance from the top of the layout box to the baseline of the
    // first line of text, and a 40.sp distance from the bottom of the layout box to the baseline
    // of the last line of text.
    val distanceToFirstBaseline = 30.sp
    val distanceFromLastBaseline = 40.sp
    Text(
        text = "This line has the first baseline.\nThis line has the last baseline.",
        modifier = Modifier.paddingFromBaseline(distanceToFirstBaseline, distanceFromLastBaseline)
    )
}
by @alexstyl