Discuss Scratch

dakoto
Scratcher
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/

Last edited by dakoto (July 16, 2014 16:25:12)

stickfiregames
Scratcher
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.
dakoto
Scratcher
47 posts

Text Wrapping - How to do properly make lines of text?

That sounds about right, thanks for your insight!
dakoto
Scratcher
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.

Last edited by dakoto (July 16, 2014 20:53:01)

falling_acorns
New Scratcher
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!
stickfiregames
Scratcher
1000+ posts

Text Wrapping - How to do properly make lines of text?

falling_acorns wrote:

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!
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.

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):

setletterto1repeatuntilletter>lengthoftextgotox:-220y:-100 start of the text boxdeleteallofcurrent wordrepeatuntilletterletteroftext= orletter>lengthoftextaddletterletteroftexttocurrent wordchangeletterby1 detect spaces or end of textifxposition+lengthofcurrent word*20<220thensetxto-220changeyby-20 height of 1 line replace 20 with width of 1 lettersetrender letterto1repeatlengthofcurrent wordswitchcostumetoletterdrawletterofcurrentwordstampchangexby20 width of 1 letterchangerender letterby1changeletterby1
12944qwerty
Scratcher
100+ posts

Text Wrapping - How to do properly make lines of text?

stickfiregames wrote:

falling_acorns wrote:

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!
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.

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):

setletterto1repeatuntilletter>lengthoftextgotox:-220y:-100 start of the text boxdeleteallofcurrent wordrepeatuntilletterletteroftext= orletter>lengthoftextaddletterletteroftexttocurrent wordchangeletterby1 detect spaces or end of textifxposition+lengthofcurrent word*20<220thensetxto-220changeyby-20 height of 1 line replace 20 with width of 1 lettersetrender letterto1repeatlengthofcurrent wordswitchcostumetoletterdrawletterofcurrentwordstampchangexby20 width of 1 letterchangerender letterby1changeletterby1

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
Scratcher
100+ posts

Text Wrapping - How to do properly make lines of text?

12944qwerty wrote:

stickfiregames wrote:

falling_acorns wrote:

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!
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.

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):

setletterto1repeatuntilletter>lengthoftextgotox:-220y:-100 start of the text boxdeleteallofcurrent wordrepeatuntilletterletteroftext= orletter>lengthoftextaddletterletteroftexttocurrent wordchangeletterby1 detect spaces or end of textifxposition+lengthofcurrent word*20<220thensetxto-220changeyby-20 height of 1 line replace 20 with width of 1 lettersetrender letterto1repeatlengthofcurrent wordswitchcostumetoletterdrawletterofcurrentwordstampchangexby20 width of 1 letterchangerender letterby1changeletterby1

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 ) but try not to necropost. If you don't know what that is, it's when you reply or post in a thread that's old. When you do this it bumps the thread to the top of the discussion forums and can bump down other posts that are more recent.

Have a nice day!
Sup1004
Scratcher
20 posts

Text Wrapping - How to do properly make lines of text?

12944qwerty wrote:

stickfiregames wrote:

falling_acorns wrote:

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!
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.

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):

setletterto1repeatuntilletter>lengthoftextgotox:-220y:-100 start of the text boxdeleteallofcurrent wordrepeatuntilletterletteroftext= orletter>lengthoftextaddletterletteroftexttocurrent wordchangeletterby1 detect spaces or end of textifxposition+lengthofcurrent word*20<220thensetxto-220changeyby-20 height of 1 line replace 20 with width of 1 lettersetrender letterto1repeatlengthofcurrent wordswitchcostumetoletterdrawletterofcurrentwordstampchangexby20 width of 1 letterchangerender letterby1changeletterby1

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
Scratcher
1000+ posts

Text Wrapping - How to do properly make lines of text?

Sup1004 wrote:

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
Scratcher
1000+ posts

Text Wrapping - How to do properly make lines of text?

Sup1004 wrote:

-snip
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.
WimpyKidFan72
Scratcher
8 posts

Text Wrapping - How to do properly make lines of text?

stickfiregames wrote:

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?

Powered by DjangoBB