Removing rows from a label.

Started by Ubel, Sat 24/06/2006 23:56:26

Previous topic - Next topic

Ubel

Okay, so let's say I have a label with a long string in it. I want to remove a certain number of lines/rows from the top of the label. So I guess I need to know how long (in characters) the lines are so I can use Substring to remove the lines.

How could I do this, when the string changes a lot in the game and the characters in the font are not all the same size?

I need this kind of script because if the string gets too long (in this case 15 rows) I need to remove some rows from the top of the label so that more text can fit in.

GarageGothic

#1
This would be way easier if you were using a monowidth font, but what you actually need to do is to create your own linebreaking code. You may even want to do all the linebreaking in code to make sure there isn't any inaccuracy.

To count the number of characters in the first line, you can do this (it is possibly to optimize it for speed, but that shouldn't be necessary with just one String):

Code: ags
String tempstring = originalstring; //making a temp string so we don't mess up the original
while (GetTextWidth(tempstring, fontnumber) > lblMyLabel.Width) {
  tempstring.Truncate(tempstring.Length - 1);
  }
originalstring.Substring(0, tempstring.Length); //removing the number of characters that was in the first line

monkey0506

Quote from: GarageGothic on Sun 25/06/2006 00:04:15
Code: ags
String tempstring = originalstring; //making a temp string so we don't mess up the original
while (GetTextWidth(tempstring, fontnumber) > lblMyLabel.Width) {
  tempstring.Truncate(tempstring.Length - 1);
  }
originalstring.Substring(0, tempstring.Length); //removing the number of characters that was in the first line


That's not quite right. String.Truncate and String.Substring don't actually edit the String, they return the edited String:

Code: ags
String tempstring = originalstring; //making a temp string so we don't mess up the original
while (GetTextWidth(tempstring, fontnumber) > lblMyLabel.Width) {
  tempstring = tempstring.Truncate(tempstring.Length - 1);
  }
originalstring = originalstring.Substring(0, tempstring.Length); //removing the number of characters that was in the first line


You had the basic idea right...and actually I know when I'm coding I forget that those functions work that way too.  I've had a lot of frustrating times caused by "my stupid computer" when really I just forgot to change the String. Then I can just laugh the whole thing off. :=

GarageGothic

Thanks monkey, I still haven't gotten entirely used to the new concept of Strings.

monkey0506

It's okay. I just didn't want Pablo to freak out ("OMG IT DOESN'T WORK YOU N00B!" :=)...but seriously, there are no functions that actually edit Strings. They return edited versions, quite a different concept compared to the old-style string functions which edited the strings.

Like I said, I've made the same mistake before, so it's not really a big deal.  Just trying to save Pablo (and you) some stress. ;)

Ubel

#5
Thanks guys. There's just one problem in this code. It doesn't work when I have "[" linebreaks in my string. And I have a lot of those, the game puts a linebreak in the string every time it adds text to it. Because then it won't remove just the first line, it can remove some characters from the line below too. And I'm not skilled enough to fix this. :)

EDIT:
Maybe it's best if I provide an example. Let's say I have a label that can show 3 rows. I have a string put into that label: "First row[Second[Third"
That would be displayed as:
Code: ags

First row
Second
Third


Now, I want to add this text "[Fourth[Fifth" to the same string. But the lines would not show in the label because the label is 3 rows high. So I'd need to remove enough rows from the top of the label so that the new lines would show in the label. Then the label would be like:
Code: ags

Third
Fourth
Fifth


This is basically what I'd want to do. I hope someone can give me some help here.

monkey0506

#6
Label.Text = Label.Text.Substring(17, Label.Text.Length - 17);

Would that work (replacing Label with the script name of your label)?

[EDIT:]

As an alternative to using 17, but assuming you know the first word of the line you want to appear on top, you could do this:

Label.Text = Label.Text.Substring(Label.Text.Contains("Third"), Label.Text.Length - Label.Text.Contains("Third"));

(Note that if the Label contains more than 200 characters this won't work as String.Contains is bugged.)

SMF spam blocked by CleanTalk