```var readMore by remember { mutableStateOf(false...
# compose
k
Copy code
var readMore by remember { mutableStateOf(false) }
var clickText by remember { mutableStateOf("Read More") }
var showClickButton by remember { mutableStateOf(false) }
val annotatedText = buildAnnotatedString {
    withStyle(style = SpanStyle(color = MaterialTheme.colors.OnSurface800)) {
        append(text + " ")
    }
    
    if (showClickButton) {
        pushStringAnnotation("click", "click")
        withStyle(style = SpanStyle(color = MaterialTheme.colors.Interactive500)) {
            append(clickText)
        }
        pop()
    }
}

ClickableText(
        text = annotatedText,
        style = MaterialTheme.typography.smallBody,
        overflow = TextOverflow.Ellipsis,
        maxLines = if (readMore) Int.MAX_VALUE else 2,
        onTextLayout = { showClickButton = it.hasVisualOverflow }
    )
I am trying to “Read More” button at the end of text view but looks like
buildAnnotatedString
does not recompose when
showClickButton
or state change, so here how can i achieve this ?
a
derivedStateOf
k
Can you please share some sample or source of how i can achieve it
k
Sorry, i don’t think it will work in my case
z
Hm, that should still work even without
derivedStateOf
(although using
derivedStateOf
would still probably be more efficient)
Is your function recomposing when the text layout changes?
k
@Zach Klippenstein (he/him) [MOD] no above code is not working
z
“not working” can mean a lot of different things 😉
i copied your code and it works as expected for me
i would put some breakpoints or log statements in and see if
onTextLayout
is getting called and giving you the values you expect