Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change the TextDecoration.Underline with a custom style?

I wanted to apply custom underline style to the underlined text. Can we change within TextDecoration.underline ??

textStyle = MaterialTheme.typography.titleMedium.copy(
    textAlign = TextAlign.Start,
    fontWeight = FontWeight.W600,
    color = color,
    textDecoration = TextDecoration.Underline
)
like image 400
Jeevan Rupacha Avatar asked Nov 16 '25 03:11

Jeevan Rupacha


1 Answers

Instead of using textDecoration = TextDecoration.Underline you can simply draw a line at the bottom of the Text.

Something like:

Text(
    modifier = Modifier.drawBehind {
        val strokeWidthPx = 1.dp.toPx()
        val verticalOffset = size.height - 2.sp.toPx()
        drawLine(
            color = Teal200,
            strokeWidth = strokeWidthPx,
            start = Offset(0f, verticalOffset),
            end = Offset(size.width, verticalOffset)
        )
    },
    text = text,
)

enter image description here

If you have a Text with more than 1 line you have to calculate text height and draw the line for each line of text.

like image 192
Gabriele Mariotti Avatar answered Nov 18 '25 16:11

Gabriele Mariotti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!