https://kotlinlang.org logo
#compose
Title
# compose
m

Mark

12/10/2023, 8:08 AM
Is there any reason why
ClickableText
does not accept an
inlineContent
arg to pass through to
BasicText
?
a

Anastasia [G]

12/11/2023, 12:40 PM
What’s your use case?
m

Mark

12/11/2023, 2:57 PM
I’m showing a
Dialog
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:
Copy code
@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 }
    )
}
Why not just add a
onOffsetClick: ((Int) -> Unit)? = null
arg to
Text
?
z

Zach Klippenstein (he/him) [MOD]

12/11/2023, 3:51 PM
ClickableText is a temporary convenience method, it’s not practical for a lot of reasons. A more robust solution is being worked on, but for now you can copy the impl of CT and customize as necessary
thank you color 1
2 Views