GWT Label widgets iterprets everything as text, not as html tags - that's good, but I would like it to interpret \n
as a <br />
how do i do that.
I would make subclass, but I cant find what to override to achieve this behaviour
(I could use HTML widget, but it would interpret all tags - and all I need is an line brak)
The <br> HTML element produces a line break in text (carriage-return).
To add spacing between lines or paragraphs of text in a cell, use a keyboard shortcut to add a new line. Click the location where you want to break the line. Press ALT+ENTER to insert the line break.
The <br> tag inserts a single line break. The <br> tag is useful for writing addresses or poems.
Use an HTML widget and set its value using a SafeHtml
constructed with SafeHtmlBuilder.appendEscapedLines
:
HTML label = new HTML(new SafeHtmlBuilder().appendEscapedLines("foo<bar\nbaz>quux").toSafeHtml());
(alternatively, you can split("\n", -1)
your text, call SafeHtml.htmlEscape
on each part and join them back with a <br>
, that's what appendEscapedLines
does)
Another option is to use CSS, which may be sufficient in some cases where this problem emerges.
Add the CSS attribute white-space: pre
or pre-wrap
in the area where you display the text. It will ensure that line breaks are respected when rendering the document.
This approach has the potential to reduce some complexity, e.g. the processing of input where \n
is replaced with <br/>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With