2 Column Label/RichTextLabel

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By andersmmg

I want to have some text automatically set to fit in 2 columns for a book. I don’t think there’s a way in the Label itself to do this, how should I go about wrapping the end to the second column?

:bust_in_silhouette: Reply From: Ertain

I suggest trying this: take a MarginContainer and have it fill your scene (maybe give it a rectangular shape). Assign an HBoxContainer as the MarginContainer node’s child and set its size property to fill the MarginContainer vertically and horizontally. Then assign two Label nodes as the children of the HBoxContainer. Set those nodes to also fill the vertical and horizontal spaces. After that, add in the text to the Label nodes and you’ll have the look you want.

That does get the look I was hoping for, I’ll just need to figure out how to split a string into the labels automatically. Thanks!

andersmmg | 2021-04-08 00:44

:bust_in_silhouette: Reply From: trisolaran

i have done something similar with a nested vbox/hbox layout, similar to what is described in the first answer. the main problem is to get the right amount of labels into the boxes: first split the text string with words = string.split(" "), which gives an array of all the word in the text. then iterate over the words-array, count chars of any word, put a label with the word in the hbox until a limit is reached, there is the linefeed and begin a new vbox. this doesnt look good, then do some fontmetrics font.get_string_size(wordlist[i]).x, count the sizes of the words do the same label stuff as above until the a limit is reached, there is the linefeed. if it still doesnt look good, then two end-limits and some calculations can make it smooth. the positive side: you can put other objects into the vbox/hbox-layout between the labels(=words). this is not possible in a richtextlabel.

I know the post is old, but I’d like to suggest another solution that doesn’t require you to browse through all the text.

For the UI layers I do something similar, but my method differs in choosing the text to display on each page.

I display all the text of the book on the left page as well as on the right page. Then, on each label, I set “text overrun behaviour” to “Trim words”, and adjust the variables “max lines visible” and above all “lines skipped” (which will be a multiple of “max lines visible”) that enable me to choose which page of my book to display.

For example, if I display 20 lines per page and want to display pages 4 and 5, page 4 will have “lines skipped” set to 60 and page 5 will have “lines skipped” set to 80.

Hope this helps!