I am trying to use a `Text` or `TextField` to disp...
# compose
r
I am trying to use a
Text
or
TextField
to display a repeating decimal, e.g., 2.(0123456789) (I am resorting to parentheses notation because to the best of knowledge, Android does not support overline, as discussed on this channel last week: https://kotlinlang.slack.com/archives/CJLTWPH7S/ ). The problem I have now is that line breaking rules will break the line after the "." (FYI it breaks up ".(" but not ".1(", for example).  I’ve been trying to look for a solution but I really only found this 9-year old one for the regular UI (and it seems like overkill in any case): https://stackoverflow.com/questions/6134457/how-to-prevent-edittext-from-breaking-a-line-after-punctuation.  What I want is a line break only when the text string (in my case digits with other characters like "(", ")", "/", "+", "-", etc.) exceeds the width of the view. Essentially I just want the line breaking rules turned off. Can this be done in Compose? (I am porting a prototype of my app that uses JavaFX, which breaks lines as I desire.)
m
What I want is a line break only when the text string (in my case digits with other characters like "(", ")", "/", "+", "-", etc.) exceeds the width of the view
That seems to be what's happening, assuming that you are the person who wrote this SO question: https://stackoverflow.com/q/65877507/115145 What you really seem to be after is to be able to supply a custom line-break implementation, whereby certain characters are treated as if they were digits. I suspect that the actual text rendering is being done by the platform (e.g., Android, Windows) not by Compose. In the case of Android, it looks like they are delegating to
BoringLayout
and `StaticLayout`: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:text/tex[…]ext/android/TextLayout.kt
r
Yes, that was my post. I guess this leaves me with a whole lot of customization to do. I'm very surprised that of all the issues I could have anticipated arising by going to Compose, text fields would be the one to break me (no pun intended).
s
Can you try to add no break whitespace in the final portion of the string where you dont want any line break to happen?
If you want to completely disable line break, use softwrap=false
If you want to have some control over where line break happens or should not happen use zero width space or no break space characters. Actually if this is a common requirement, we can have some SpanStyle s in compose that does it for you. I'd appreciate if you created a ticket as well so that we can add to our plans.
r
Thanks @Siyamed. I had tried softWrap=false (despite the warning "if softWrap is false, overflow and TextAlign may have unexpected effects"). It did keep the ".(" together, but once the width of the view was hit, all the remaining digits were cut off (that is, not displayed).
I just tried "zero width no-break space" (\uFEFF) and that seems to work. So if I add the no-break space wherever a break might occur in my inputs, that would fix it? And theoretically, If I added it after every character, that would guarantee no breaks (except for at view width)? I will experiment with that in the meantime.
I can write a ticket too. Also, I would write one for "overline" support on TextDecoration if you thought that was worth asking for (or doable).
Thanks for suggesting non-breaking space as a workaround. I identified all cases of line breaks and insert the non breaking space into the string passed to Text (leaving the "hoisted" state of the string unchanged). This should keep me rolling. Thanks again!
👍 1
s
happy to hear that it worked
r
Feature request (though I did not know how to label it that way) for disabling line breaks is here: https://issuetracker.google.com/u/3/issues/178614928