Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Text Wrapping - How to do properly make lines of text?
- dakoto
-
47 posts
Text Wrapping - How to do properly make lines of text?
So in my scenario, I was able to create a text engine, where I can display text on the screen via using lists and length of the item in the list. When the text is long enough so it hits the end of the screen, the text continues to display a few Y pixels lower than the first row.
My problem is: The last word before the Y shift is cut short and the rest of the word is spelled the line below it.
My question is: How do I create a code so that the word is not cut off? Also known as text wrapping?
My general idea: Do I just make a variable that counts ahead of the current text reader, looking for “spaces” between the words, and count how many letters are in between?
Here's my project for reference:
http://scratch.mit.edu/projects/22479558/
My problem is: The last word before the Y shift is cut short and the rest of the word is spelled the line below it.
My question is: How do I create a code so that the word is not cut off? Also known as text wrapping?
My general idea: Do I just make a variable that counts ahead of the current text reader, looking for “spaces” between the words, and count how many letters are in between?
Here's my project for reference:
http://scratch.mit.edu/projects/22479558/
Last edited by dakoto (July 16, 2014 16:25:12)
- stickfiregames
-
1000+ posts
Text Wrapping - How to do properly make lines of text?
It's quite complicated to make text wrap, but I think it could be done.
First, you would need a variable to store the length of the current line, in pixels. You would also need to know how wide each letter is.
At the start, or every time a space is reached, the script would add up the widths of each letter in the next word, and work out if there was enough space on the current line. If there is, it would add it to that line and increase the current length variable. Otherwise, it would start a new line and reset the variable.
So that is the basic idea, let me know if you want to know more. Of course, if you already know what it will type then you could hardcode the line breaks instead.
First, you would need a variable to store the length of the current line, in pixels. You would also need to know how wide each letter is.
At the start, or every time a space is reached, the script would add up the widths of each letter in the next word, and work out if there was enough space on the current line. If there is, it would add it to that line and increase the current length variable. Otherwise, it would start a new line and reset the variable.
So that is the basic idea, let me know if you want to know more. Of course, if you already know what it will type then you could hardcode the line breaks instead.
- dakoto
-
47 posts
Text Wrapping - How to do properly make lines of text?
That sounds about right, thanks for your insight!
- dakoto
-
47 posts
Text Wrapping - How to do properly make lines of text?
Alright, I have successfully made text wrapping for my game! Thanks again!
Edit: That is, without the use of hardcoding my scripts to do so.
Edit: That is, without the use of hardcoding my scripts to do so.
Last edited by dakoto (July 16, 2014 20:53:01)
- falling_acorns
-
1 post
Text Wrapping - How to do properly make lines of text?
SFG, can you tell me more about hardcoding the line breaks? I have multiple text lengths as items in a list in data and I'm not seeing a way to hardcode the line breaks within each item.
Can you tell me more about figuring out the length of the current line, in pixels as well as figuring out how wide each letter is? I'm guessing this will be the better way to handle my variable text length items in my list complied in data.
Thanks!
Can you tell me more about figuring out the length of the current line, in pixels as well as figuring out how wide each letter is? I'm guessing this will be the better way to handle my variable text length items in my list complied in data.
Thanks!
- stickfiregames
-
1000+ posts
Text Wrapping - How to do properly make lines of text?
Probably the easiest way to hardcode the breaks is just by trial and error. Render each item without line breaks, and the first time a word goes over the end of a line, put a line break before it (it is best to use a punctuation character that will not be in the text, and make the text engine start a new line if it reaches that character). Repeat this until the text all renders properly. SFG, can you tell me more about hardcoding the line breaks? I have multiple text lengths as items in a list in data and I'm not seeing a way to hardcode the line breaks within each item.
Can you tell me more about figuring out the length of the current line, in pixels as well as figuring out how wide each letter is? I'm guessing this will be the better way to handle my variable text length items in my list complied in data.
Thanks!
To calculate the length of each word, it is easiest if you have all letters the same width. The script would be something like this (I haven't tested it, so you might need to change it a bit, and you will also need to change some numbers depending on the size of your text):
- 12944qwerty
-
100+ posts
Text Wrapping - How to do properly make lines of text?
Probably the easiest way to hardcode the breaks is just by trial and error. Render each item without line breaks, and the first time a word goes over the end of a line, put a line break before it (it is best to use a punctuation character that will not be in the text, and make the text engine start a new line if it reaches that character). Repeat this until the text all renders properly. SFG, can you tell me more about hardcoding the line breaks? I have multiple text lengths as items in a list in data and I'm not seeing a way to hardcode the line breaks within each item.
Can you tell me more about figuring out the length of the current line, in pixels as well as figuring out how wide each letter is? I'm guessing this will be the better way to handle my variable text length items in my list complied in data.
Thanks!
To calculate the length of each word, it is easiest if you have all letters the same width. The script would be something like this (I haven't tested it, so you might need to change it a bit, and you will also need to change some numbers depending on the size of your text):
Wow, the visual idea really helps! Thank you
How do you know the width? Is it the size of the letter?
One thing, It shouldn't work because you keep resetting the text to one place. And that where is the draw letter from?
Last edited by 12944qwerty (Feb. 28, 2018 23:22:50)
- Project_1001
-
100+ posts
Text Wrapping - How to do properly make lines of text?
Probably the easiest way to hardcode the breaks is just by trial and error. Render each item without line breaks, and the first time a word goes over the end of a line, put a line break before it (it is best to use a punctuation character that will not be in the text, and make the text engine start a new line if it reaches that character). Repeat this until the text all renders properly. SFG, can you tell me more about hardcoding the line breaks? I have multiple text lengths as items in a list in data and I'm not seeing a way to hardcode the line breaks within each item.
Can you tell me more about figuring out the length of the current line, in pixels as well as figuring out how wide each letter is? I'm guessing this will be the better way to handle my variable text length items in my list complied in data.
Thanks!
To calculate the length of each word, it is easiest if you have all letters the same width. The script would be something like this (I haven't tested it, so you might need to change it a bit, and you will also need to change some numbers depending on the size of your text):
Wow, the visual idea really helps! Thank you
How do you know the width? Is it the size of the letter?
One thing, It shouldn't work because you keep resetting the text to one place. And that where is the draw letter from?
Glad you appreciated his post, (I too, found it helpful

Have a nice day!

- Sup1004
-
20 posts
Text Wrapping - How to do properly make lines of text?
Probably the easiest way to hardcode the breaks is just by trial and error. Render each item without line breaks, and the first time a word goes over the end of a line, put a line break before it (it is best to use a punctuation character that will not be in the text, and make the text engine start a new line if it reaches that character). Repeat this until the text all renders properly. SFG, can you tell me more about hardcoding the line breaks? I have multiple text lengths as items in a list in data and I'm not seeing a way to hardcode the line breaks within each item.
Can you tell me more about figuring out the length of the current line, in pixels as well as figuring out how wide each letter is? I'm guessing this will be the better way to handle my variable text length items in my list complied in data.
Thanks!
To calculate the length of each word, it is easiest if you have all letters the same width. The script would be something like this (I haven't tested it, so you might need to change it a bit, and you will also need to change some numbers depending on the size of your text):
Wow, the visual idea really helps! Thank you
How do you know the width? Is it the size of the letter?
One thing, It shouldn't work because you keep resetting the text to one place. And that where is the draw letter from?
WOW! Thanks Soo much!
Edit: MY FIRST POST!!!

Last edited by Sup1004 (Aug. 27, 2020 18:49:05)
- Wyan100
-
1000+ posts
Text Wrapping - How to do properly make lines of text?
WOW! Thanks Soo much!
Edit: MY FIRST POST!!!
Please don't post on old topics, also please don't let your quotes get to long.
Also: Welcome to the Forums! Hope you enjoy your stay, and don't touch the flames!
- RL1123
-
1000+ posts
Text Wrapping - How to do properly make lines of text?
Welcome to the forums, but please do not necropost. Necroposting is bumping old topics to the front page and the other unresolved topics get bumped down. Please refrain from doing this in the future -snip

- WimpyKidFan72
-
8 posts
Text Wrapping - How to do properly make lines of text?
It's quite complicated to make text wrap, but I think it could be done.
First, you would need a variable to store the length of the current line, in pixels. You would also need to know how wide each letter is.
At the start, or every time a space is reached, the script would add up the widths of each letter in the next word, and work out if there was enough space on the current line. If there is, it would add it to that line and increase the current length variable. Otherwise, it would start a new line and reset the variable.
So that is the basic idea, let me know if you want to know more. Of course, if you already know what it will type then you could hardcode the line breaks instead.
WOW! How did you put images that took you to the right links?
- Discussion Forums
- » Help with Scripts
-
» Text Wrapping - How to do properly make lines of text?