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

Devesh Sanghvi

10/15/2020, 11:27 PM
How do we display strikethrough and bold text in Text component? I tried SpannableString and Html tags string but it does not seem to work.
s

Siyamed

10/15/2020, 11:28 PM
you set the TextStyle attributes in Text()
we dont accept SpannableString
we dont have HTML conversion
AnnotatedString is the equivelant of SpannableString if you need to strike through only a portion of the text
d

Devesh Sanghvi

10/15/2020, 11:31 PM
Yes I need to strike through and bold only part of the string. If I go with TextStyle attribute then I would need multiple Text() since this attribute will apply to whole string and not part of the string. Let me try with AnnotatedString. Thanks
n

nglauber

10/16/2020, 1:47 AM
@Devesh Sanghvi not sure if you find out already, but I did this way… not sure if it is the best way though…
Copy code
@Composable
fun TextSample() {
    val builder = AnnotatedString.Builder().apply {
        append(
            AnnotatedString(
                "Text Bold",
                SpanStyle(fontWeight = FontWeight.Bold)
            )
        )
        append(
            AnnotatedString(
                "Text Strike",
                SpanStyle(textDecoration = TextDecoration.LineThrough)
            )
        )
    }
    Text(text = builder.toAnnotatedString())
}
m

Mohamed Elfiky

10/16/2020, 2:05 AM
how to apply AnnotatedString to TextField?
s

Siyamed

10/16/2020, 2:42 AM
We dont support annotated string in TextField yet
m

Mohamed Elfiky

10/16/2020, 2:42 AM
is VisualTransformation the way to go write now?
s

Siyamed

10/16/2020, 5:47 AM
We added visualtransformation for formatting (password credit card, etc) but apperantly works for now. I never tried it, we will add the support but not now :/
d

Devesh Sanghvi

10/16/2020, 5:23 PM
@nglauber Yes I got it working something like this.
Copy code
with(AnnotatedString.Builder("Text Strike")) {
    addStyle(SpanStyle(textDecoration = TextDecoration.LineThrough), 0, charges.length)

    pushStyle(SpanStyle(fontWeight = FontWeight.Bold))

    append("Text Bold")

    toAnnotatedString()
}
👏 1
@Siyamed Thanks for the AnnotatedString suggestion
18 Views