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

K Merle

08/22/2022, 1:40 PM
In
Text
composable, if word does not fit in the line, it goes to the next line. Is it possible to still leave the word in the same line and have that hard break of the word? Example:
Copy code
Lorem Ipsum is simply dummy text of the 
printing and typesetting industry. Lore
m Ipsum has been the industry's standar
d dummy text ever since the 1500s.
z

Zach Klippenstein (he/him) [MOD]

08/22/2022, 1:48 PM
We’re in the process of designing a line breaking API, but as of right now i don’t believe there’s any way to get the Text composable to do this.
k

K Merle

08/22/2022, 3:32 PM
Seems like replacing white space char, with a no-line-break characters does the trick of keeping text in a same line (example above). https://en.wikipedia.org/wiki/Whitespace_character
r

Rick Regan

08/22/2022, 7:11 PM
I have been doing this and it works well. I put a “\uFEFF” after characters like space, period, and forward slash. Check out this feature request I wrote in Jan 2021 (and star if you like 😀).
a

Alex Vanyo

08/22/2022, 11:51 PM
That’s a neat trick:
Copy code
val sourceText = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s."
val modifiedText = sourceText.toList().joinToString("\u200B")
Text(modifiedText)
r

Rick Regan

08/22/2022, 11:59 PM
If I use
" \u200B"
instead of
"\uFEFF"
in my code I don’t get the non-breaking behavior.
a

Alex Vanyo

08/23/2022, 12:04 AM
Is the space there intentional?
\uFEFF
also works for me, although the Wikipedia article says that one is deprecated?
\u2060
also works. I’m just trying all of these out for fun, not sure how supported this is or if this is a reasonable approach. There’s also
TransformedText
and
VisualTransformation
, which might be the correct thing to do for something like this
r

Rick Regan

08/23/2022, 1:01 AM
The space was part of my quick and dirty test to replace space with space+non-breaking space (I neglected to remove it before I pasted it here).
I did not know
\uFEFF
was deprecated! (Thanks.) Maybe I will switch to
\u2060
, which appears to works as well.
What’s interesting is that sometime in the last year and a half plus that I’ve been using Compose the forward slash and the period no longer cause a line break without the non-breaking space (if we weren’t playing around with it right now I never would have known). I wonder what could have changed? I have a restricted set of special characters that my
Text
uses, and at the time I experimented with all of them (a left or right parenthesis, for example, never caused a problem — and still don’t). Space, period, and forward slash were the three that I needed to insert the non-breaking space after. Now it appears only space requires it. (I don’t know that I would remove the code in my app that inserts them, just to be safe — unless I got a definitive reason why the behavior changed.)
And thanks — I will look into
TransformedText
as the better way to do this.
k

K Merle

08/23/2022, 5:10 AM
@Rick Regan Have you by any chance been adding hyphens at the end of lines? I've been experimenting yesterday, and can't make it perfect for now.
s

Siyamed

08/23/2022, 5:13 AM
I didn't understand the problem/question :/ Is the screenshot desired or non-desired output?
I don't think in conpose line breaking changed intentionally. It might be android usage and some tricks we played on staticlayout having a side effect.
k

K Merle

08/23/2022, 5:17 AM
@Siyamed Code block was a desired output.
r

Rick Regan

08/23/2022, 12:49 PM
K Merle: I have not used hyphens (they’re not part of my limited input characters).
Siyamed: The desired behavior in this case is only line-breaking forced by the end of the
Text
. The use of the non-breaking space for the few problematic symbols was how I dealt with it. Having fewer symbols to account for is better in some sense, but still I’d like to know if it’s a bug or a feature. I’ll see if I can figure out when it changed (I’m on the latest Compose and Material 3 currently, and API 33 for that matter).
s

Siyamed

08/23/2022, 2:25 PM
Got it. The need is allow break at letter boundaries instead of words.
r

Rick Regan

08/23/2022, 2:28 PM
And in my case specifically, the “words” are numeric expressions, and the “letters” are digits and symbols.
After looking into it I think it was my code that changed (
Text
width) and not line breaking behavior of forward slash and period. Sorry for the false alarm.
9 Views