Mark
01/25/2024, 1:12 PMText
with InlineTextContent
are no longer laid out properly. The inline images are often large (screenshots) and are now simply overlayed on top of the text content instead of placed between preceding and following text. I’m using these Text
composables in Dialogs
in case that is relevant.Halil Ozercan
01/25/2024, 2:14 PMMark
01/26/2024, 5:46 AMval annotation = "my_annotation"
val text = buildAnnotatedString {
appendLine("image below text in 2023.10.01 but covers text in 2024.01.00")
appendLine("Note: make sure image is taller than available height of Text composable")
append('\uffff')
addStringAnnotation(tag = "androidx.compose.foundation.text.inlineContent", annotation, length - 1, length)
}
val bitmap = ContextCompat.getDrawable(LocalContext.current, R.drawable.screenshot)!!.toBitmap().asImageBitmap()
val placeholder = with(LocalDensity.current) {
Placeholder(bitmap.width.toSp(), bitmap.height.toSp(), PlaceholderVerticalAlign.Center)
}
val inlineTextContent = InlineTextContent(placeholder) {
Image(bitmap = bitmap, contentDescription = null)
}
Text(text = text, inlineContent = mapOf(annotation to inlineTextContent))
Add a screenshot.png
to drawable-nodpi
and make sure it is taller than available height of Text
composable
@Halil Ozercan Note: nothing to do with Dialog
, this problem occurs wherever I place the Text
Halil Ozercan
01/26/2024, 10:56 AMLineHeight
value in your theme's typography? One way to understand whether Theme effects anything at all is to pass style = TextStyle.Default
to your Text
composable.Mark
01/26/2024, 12:09 PMstyle
parameter, the problem goes away. The theme I’m using is MdcTheme
.Halil Ozercan
01/26/2024, 12:11 PMAnnotatedString
val annotation = "my_annotation"
val paragraphStyle = LocalTextStyle.current.toParagraphStyle()
val spanStyle = LocalTextStyle.current.toSpanStyle()
val text = buildAnnotatedString {
withStyle(spanStyle) {
withStyle(paragraphStyle) {
appendLine("image below text in 2023.10.01 but covers text in 2024.01.00")
appendLine("Note: make sure image is taller than available height of Text composable")
}
withStyle(paragraphStyle.copy(lineHeight = TextUnit.Unspecified)) {
append('\uffff')
addStringAnnotation(tag = "androidx.compose.foundation.text.inlineContent", annotation, length - 1, length)
}
}
}
val bitmap = ContextCompat.getDrawable(LocalContext.current, R.drawable.screenshot)!!.toBitmap().asImageBitmap()
val placeholder = with(LocalDensity.current) {
Placeholder(bitmap.width.toSp(), bitmap.height.toSp(), PlaceholderVerticalAlign.Center)
}
val inlineTextContent = InlineTextContent(placeholder) {
Image(bitmap = bitmap, contentDescription = null)
}
Text(text = text, inlineContent = mapOf(annotation to inlineTextContent), style = TextStyle.Default)
Halil Ozercan
01/26/2024, 12:12 PMlineHeight
is a strong constraint against inline content. Placeholders cannot grow taller than the constrained line height.Mark
01/26/2024, 12:13 PMHalil Ozercan
01/26/2024, 12:14 PM1.4.0
, lineHeight has always been an issue.Mark
01/26/2024, 12:15 PMMdcTheme
?Mark
01/26/2024, 12:17 PMInlineTextContent
mechanism (in which case only apply lineHeight contraints for text) or have an InlineNonTextContent
mechanism or something.Mark
01/26/2024, 12:47 PMstyle = _LocalTextStyle_.current.merge(lineHeight = TextUnit.Unspecified, lineHeightStyle = null)
but this doesn’t workHalil Ozercan
01/26/2024, 12:47 PMMark
01/26/2024, 12:49 PMstyle = _LocalTextStyle_.current.copy(lineHeight = TextUnit.Unspecified)
Mark
01/27/2024, 3:24 AM