Skip to content

Instantly share code, notes, and snippets.

@parthdesai1208
Last active March 8, 2022 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parthdesai1208/025200014969b2176a70eea82d710f99 to your computer and use it in GitHub Desktop.
Save parthdesai1208/025200014969b2176a70eea82d710f99 to your computer and use it in GitHub Desktop.
compose custom view
fun Modifier.baseLineToTop(firstBaselineToTop: Dp): Modifier {
return this.then(
layout { measurable, constraints ->
val placeable = measurable.measure(constraints = constraints)
// Check the composable has a first baseline
check(placeable[FirstBaseline] != AlignmentLine.Unspecified)
val firstBaseline = placeable[FirstBaseline]
// now we minus total padding from firstbaseline, so we get it from baseline
val placeableY = firstBaselineToTop.roundToPx() - firstBaseline
// now we have to draw box with height of the layout + value that we get from minus operation
val height = placeable.height + placeableY
//apply height to layout
layout(placeable.width, height) {
// we place layout in box
placeable.placeRelative(0, placeableY)
}
}
)
}
use it like,
Text("Hi there!", Modifier.baseLineToTop(32.dp))
@parthdesai1208
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment