Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack compose Justified and RTL Text

In jetpack Compose, you can Justify a Text like this:

Text(
       text = text,
       textAlign = TextAlign.Justify
    )

If you want support RTL, you achive this by:

Text(
       text = text,
       textAlign = TextAlign.Right
    )

How can a Text() support RTL text and justify it same time in Jetpack Compose?

like image 907
Kaaveh Mohamedi Avatar asked Sep 18 '25 19:09

Kaaveh Mohamedi


2 Answers

After several hour testing, I reach this:

@Composable
fun JustifiedRTLText(
    text: String,
    modifier: Modifier = Modifier
) {
    CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {
        Text(
            text = text,
            textAlign = TextAlign.Justify,
            modifier = modifier,
        )
    }
}
like image 180
Kaaveh Mohamedi Avatar answered Sep 20 '25 08:09

Kaaveh Mohamedi


In Jetpack Compose RTL or LTR will automatically set by according to the text content. But we can force it by changing the textDirection of style to TextDirection.Content or TextDirection.RTL

Text(
    text = text,
    textAlign = TextAlign.Justify,
    style = TextStyle(textDirection = TextDirection.Content)
)
like image 24
Muhammad A R Avatar answered Sep 20 '25 10:09

Muhammad A R