Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose Text Hyphenation

How do I get words to break correctly to new lines in Android with Jetpack Compose? I know the functionality from the web, where I use ­ for such cases.

I defined string values with possible line breaks like this: Korrespondenz\u00ADsprache. Unfortunately this does not work for Android.

I use the following code

Text(
    text = "Korrespondenz\u00ADsprache",
    style = MaterialTheme.typography.h4
)

Currently the result looks like this:

enter image description here

The expected result should look like this:

enter image description here

like image 548
Benjamin Lüscher Avatar asked Jun 09 '26 06:06

Benjamin Lüscher


2 Answers

You can override the default configuration of the hyphenation configuration (Hyphens = Hyphens.None) using:

   Text(
       text = "Korrespondenz\u00ADsprache",
       style = MaterialTheme.typography.h4.copy(hyphens = Hyphens.Auto)
   )

enter image description here

With Auto the words will be automatically broken at appropriate hyphenation points.

It requires 1.3.0-rc01

like image 185
Gabriele Mariotti Avatar answered Jun 10 '26 19:06

Gabriele Mariotti


Looks like hyphen is just recently supported in Compose 1.3.0-rc01 release candidate as part of Experimental API

https://developer.android.com/jetpack/androidx/releases/compose-ui#1.3.0-rc01

And you might expect something like this in TextStyle parameters in that compose version

(
...
 hyphens = Hyphens.Auto
...
)
like image 20
z.g.y Avatar answered Jun 10 '26 19:06

z.g.y



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!