Put sentences in Label Caption


I want to input sentences as Label Caption on run time.
The idea is to break the sentences in several lines with LineEnding.

This is what I have, but obviously not what it should be.
I want the lines to be of a certain length instead of 10 words per line.
Also the maximum C variable for looping should be not higher than the number of words.

ReadString := ‘Welcome sir, I hope you had a pleasant journey. I will guide you to the base from here but feel free to ask me to guide you around the island also.’;

ReadString:= MidStr(ReadString, 0, Length(ReadString));

for C := 1 to 500 do
EndPos := PosEx(’ ', ReadString, StartPos);
Word[C] := MidStr(ReadString, StartPos, EndPos - StartPos);
StartPos := EndPos + 1;

TalkTextLabel.Caption :=
Word[1] + Word[2] + Word[3] + Word[4] + Word[5] + Word[6] + Word[7] + Word[8] + Word[9] + Word[10] + LineEnding +
Word[11] + Word[12] + Word[13] + Word[14] + Word[15] + Word[16] + Word[17] + Word[18] + Word[19] + Word[20] + LineEnding +
Word[21] + Word[22] + Word[23] + Word[24] + Word[25] + Word[26] + Word[27] + Word[28] + Word[29] + Word[30] + LineEnding +
Word[31] + Word[32] + Word[33] + Word[34] + Word[35] + Word[36] + Word[37] + Word[38] + Word[39] + Word[40] + LineEnding +
Word[41] + Word[42] + Word[43] + Word[44] + Word[45] + Word[46] + Word[47] + Word[48] + Word[49] + Word[50] + LineEnding;

@Carring Hi and welcome! :slight_smile:

I’m not 100% sure if I understood your question correctly.

TCastleLabel automatically splits the text into lines, just specify a proper MaxWidth https://castle-engine.io/apidoc-unstable/html/CastleControls.TCastleLabel.html#MaxWidth - it’ll automatically break the line at words.

If for some reason you want to do that manually try SplitString function https://castle-engine.io/apidoc-unstable/html/CastleStringUtils.html#SplitString to split the sentence into words. Then you can use TCastleFont.TextWidth https://castle-engine.io/apidoc-unstable/html/CastleFonts.TCastleFont.html#TextWidth to determine the length of every word or some sentence fragments (e.g. to see if it exceeds the line width or not).

1 Like

Thanks! That was more easy than I thought. I did not know the words were automatically split with maxwidth so I was looking for the spaces between them.

1 Like