Mark
12/10/2023, 8:08 AMClickableText
does not accept an inlineContent
arg to pass through to BasicText
?Anastasia [G]
12/11/2023, 12:40 PMMark
12/11/2023, 2:57 PMDialog
with images and clickable spans. The dialog content comes from an AnnotatedString
. I also noticed that ClickableText
is not doing quite the same styling as Text
. So I ended up writing my own by just merging the code from both composables:
@Composable
fun ClickableText(
text: AnnotatedString,
modifier: Modifier = Modifier,
color: Color = Color.Unspecified,
fontSize: TextUnit = TextUnit.Unspecified,
fontStyle: FontStyle? = null,
fontWeight: FontWeight? = null,
fontFamily: FontFamily? = null,
letterSpacing: TextUnit = TextUnit.Unspecified,
textDecoration: TextDecoration? = null,
textAlign: TextAlign? = null,
lineHeight: TextUnit = TextUnit.Unspecified,
overflow: TextOverflow = TextOverflow.Clip,
softWrap: Boolean = true,
maxLines: Int = Int.MAX_VALUE,
minLines: Int = 1,
inlineContent: Map<String, InlineTextContent> = mapOf(),
onTextLayout: (TextLayoutResult) -> Unit = {},
style: TextStyle = LocalTextStyle.current,
onClick: (Int) -> Unit,
) {
val layoutResult = remember { mutableStateOf<TextLayoutResult?>(null) }
val pressIndicator = Modifier.pointerInput(onClick) {
detectTapGestures { pos ->
layoutResult.value?.let { layoutResult ->
onClick(layoutResult.getOffsetForPosition(pos))
}
}
}
val localContentColor = LocalContentColor.current
val localContentAlpha = LocalContentAlpha.current
val overrideColorOrUnspecified = if (color.isSpecified) {
color
} else if (style.color.isSpecified) {
style.color
} else {
localContentColor.copy(localContentAlpha)
}
BasicText(
text = text,
modifier = modifier.then(pressIndicator),
style = style.merge(
fontSize = fontSize,
fontWeight = fontWeight,
textAlign = textAlign,
lineHeight = lineHeight,
fontFamily = fontFamily,
textDecoration = textDecoration,
fontStyle = fontStyle,
letterSpacing = letterSpacing
),
onTextLayout = {
layoutResult.value = it
onTextLayout(it)
},
overflow = overflow,
softWrap = softWrap,
maxLines = maxLines,
minLines = minLines,
inlineContent = inlineContent,
color = { overrideColorOrUnspecified }
)
}
Mark
12/11/2023, 2:57 PMonOffsetClick: ((Int) -> Unit)? = null
arg to Text
?Zach Klippenstein (he/him) [MOD]
12/11/2023, 3:51 PM