Last active
March 8, 2022 11:21
-
-
Save parthdesai1208/025200014969b2176a70eea82d710f99 to your computer and use it in GitHub Desktop.
compose custom view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Author
parthdesai1208
commented
Mar 8, 2022
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment