I was trying to create custom text rendering composable using the the Canvas composable. However, I don't understand how TextMeasurer is calculating the final height of the composable.
Here is a minimal example of what I have tried till now.
@Composable
fun ShyText(
text: String,
modifier: Modifier = Modifier
) {
val textMeasurer = rememberTextMeasurer()
val measuredText =
textMeasurer.measure(
AnnotatedString(text),
overflow = TextOverflow.Ellipsis,
constraints = Constraints.fixedWidth(1080),
style = TextStyle(
fontSize = 18.sp,
)
)
Canvas(
Modifier
.fillMaxWidth()
.height(measuredText.size.height.dp) //I think the bug is coming from this line.
.background(Color.DarkGray)
) {
drawText(
measuredText,
)
}
}
Here is how the composable is used.
ShyTextTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Column(Modifier.padding(innerPadding)) {
ShyText(
text = sampletext,
)
Text("text")
}
}
}
I am trying to render without the blank space above "text" and the final line of the paragraph. I would like to know why the blank space is being rendered. (The grey background is just for testing)
