How can I balance text lines in flutter? - flutter

I am trying to achieve something similar to this in flutter.
Take the following text for example;
Some long text that should be displayed in
two lines.
I want it to be displayed like this;
Some long text that should
be displayed in two lines.
I can't simply do this by putting a 'new line character' \n to the string manually because it will be a dynamic string.

You can use RichText, or try to add "\n" operator. So it should look like "Some long text that should\nbe displayed in two lines."

Related

How to set width of text marks in vega-lite / altair (text wrapping and line break) for long text

I am trying to display text over time using altair (vega-lite), which works fine using a layered chart, where one is created using the alt.Chart().mark_text() function to display the text.
The text though is multiple phrases and should be wrapped (with line breaks). How can this be done?
(I do not want to use fixed line breaks, e.g. \n, at distinct positions since the text wrapping should work with zooming too)
Not exactly what you want, but you can specify a character on which to break to a new line. When zooming, the text stays the same size, so it should always fit the view.
For example making a new line for every word:
.mark_text(lineBreak=' ')

Hide certain characters in a text field in Flutter

I have a text field which I need to style for example with bold or italics parts.
I tried overridding the TextEditingController's buildTextSpan and formatting the text using annotation ranges with custom styles but the edge cases were too much and I really couldn't get it to work.
So, thought about using a formatter where before every change in format I'll add a custom character, like this:
This text is |bBOLD and this is |iITALICS. Would get me this:
This text is BOLD and this is ITALICS. So I override the buildTextSpan to build the TextSpan from a parse function where I split the text by the special characters and check the initial letter of each text for formatting info.
This works well except for the fact that when I press the right arrow to go the next character after the "This text is ", the cursor will stay fixed as it thinks there are two characters but being only for formatting, they aren't there on the render.
Is there any way I could tell the textfield to ignore certain characters when selecting, moving selection or typing?
I think this would work!
static const kCharToBEIgnored = 0x2C;
// Here 0x2C means ',' comma
// For complete list visit https://api.flutter.dev/flutter/charcode/charcode-library.html
String get text {
return String.fromCharCodes(
_value.text.codeUnits.where((ch) => ch != kCharToBEIgnored),
);
}

How to indent if Text is softWrapped in Flutter

I'm working on an app that displays lyrics for several hymns in my native language.
Each line of the hymn is an individual Text Widget. I want the text to indent if it got softWrapped if user increases the font size to indicate that its not the next line. How can I achieve this effect?
Adding one or more tab escape characters "\t" to your strings may do the trick for you:
Text("\t this is a text string that will have an indentation",),

How to make a very long non-stop string (without any space in it) break into many new lines when showing in HTMLPanel?

I have an HTMLPanel inside a FlowPanel which is inside a ScrollPanel like this.
<g:ScrollPanel>
<g:FlowPanel>
<g:HTMLPanel ui:field="showMessageHTMLPanel" width="600px"/>
</g:FlowPanel>
</g:ScrollPanel>
The showMessageHTMLPanel is used to hold the text that user enter a TextArea.
I want that the showMessageHTMLPanel should show exactly like how it was displayed in TextArea.
Ex,if user types in many sentences in new lines in TextArea, then the showMessageHTMLPanel should show similar like this:
This is text1
This is text2.
So here is what I did. I uses new SafeHtmlBuilder().appendEscapedLines(message).toSafeHtml().
HTML showMessageHTML = new HTML(new SafeHtmlBuilder().appendEscapedLines(message).toSafeHtml());
getView().getShowMessageHTMLPanel().add(showMessageHTML);
The result is that It breaks the lines quite OK, no problem.
However, When I type a non-stop very long string ( a String that doesn't have any space on it) into a TextArea. Ex, see this non-stop string:
"Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa."
As you saw, even this non-stop string has no space, but when typing in TextArea then the string will automatically fall into new lines.
Ok, now when I show that non-stop string in getView().getShowMessageHTMLPanel(), it showed the text as one straight line without any line break:
Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.
The user has to scroll the scrollbar to see the complete text. This is unacceptable since it is too hard to see the whole line. Also many urls are non-stop string without spaces. So the user may not be able to copy the url.
How to make a very long non-stop string (without any space in it) break into many new lines when showing in HTMLPanel?
Or
Do you know any other widget that can handle this?
I am sure, a simple css property should help.
word-wrap:break-word;
Detail here

Multi-line button labels

Is there an easy way to have two lines of button.text where you specify each line individually? Also, there seem to be large margins on the buttons so the text font needs to be quite small to fit. Is there a way to allow the text to use more of the button area?
The way I would do a two-line text implementation is:
Dim t1,t2 As String
t1="This is line one"
t2="This is line two"
...
MyButton.Text = t1 & CRLF & t2
The CRLF performs a Carriage Return and Line feed thus splitting the text up
I can't help with the padding issue though. Have you tried changing the font size?
MyButton.TextSize=9