I'm trying to make a custom text display function that puts text on a GUI with a fixed size. I want to cut off the end of any string that gets too long for the GUI, but in order to do that I need to be able to test the string's width. I could test the length in number of characters, but it would mean having to use a font in which all characters are the same size. I thought I would use the GetTextWidth function like so:
function TextIt (String sa, int width) {
int beginning=0;
int end=sa.Length;
while (width < GetTextWidth("%s", sa.Substring(beginning, end), eFontFont1)){
end--;
}
Display("%s", sa.Substring(beginning, end));
}
But it won't compile. It seems GetTextWidth doesn't work with any sort of string formatting. Is that an accurate assessment or am I doing something wrong? If the former, is there any other way to test the width of a string in number of pixels instead of number of characters?
Not all functions are capable of formatting Strings directly, though you can pass String.Format as a parameter:
function TextIt (String sa, int width) {
int beginning=0;
int end=sa.Length;
while (width < GetTextWidth(String.Format("%s", sa.Substring(beginning, end)), eFontFont1)){
end--;
}
Display("%s", sa.Substring(beginning, end));
}
Also, just as a note, GetTextWidth is not always 100% accurate when it comes to the GUI control's automatic text wrapping. If you get slightly anomalous results it sometimes helps to pad your calcuations by a few pixels.
Also -- is there a reason you are using string formatting at all? Couldn't you just do:
function TextIt (String sa, int width) {
int beginning=0;
int end=sa.Length;
while (width < GetTextWidth(sa.Substring(beginning, end), eFontFont1)){
end--;
}
Display(sa.Substring(beginning, end));
}
That is, I don't really see what you are doing with the string formatting ("%s") at all there, since you are just using strings all the way through.
Thank you both so much. The codes work great. It never occurred to me that that I could just plug a predefined string in without using the "%s" first. I always saw it used that way in the manual, albeit with other text involved.
I will also keep your advice in mind, regarding padding.
Thanks.
I was just mindlessly answering the question without thinking it through. Of course he doesn't need to format that... ::) GetTextWidth always returns the exact width of the text when rendered in the specified font...AGS just doesn't always wrap the text correctly. Especially if there's no spaces. := (It's a known issue)
[EDIT:]
Yes, frission's post below is correct. Except that it would be possible to mash a bunch of Strings together without formatting....
String mashed = str1.Append(str2.Append(str3.Append(str4))));
It's just easier to use:
String mashed = String.Format("%s%s%s%s", str1, str2, str3, str4);
:P
Quote from: Lyaer on Mon 15/10/2007 02:08:59
Thank you both so much. The codes work great. It never occurred to me that that I could just plug a predefined string in without using the "%s" first. I always saw it used that way in the manual, albeit with other text involved.
If the function just takes a straight String object, and you are just passing it a straight String object, then you don't need to use the %s formatting. String.Format and the %s formatting in general is useful when you need to either quickly throw together a lot of variables into a String, or to combine different variable types (like putting numbers into Strings -- which is why the manual has a bunch of examples with it, since displaying scores and other common things like that requires putting ints into Strings). Sometimes it is useful for mashing together a lot of Strings as well, since they can't be just added to each other line numbers.
So this is solved now? If so, please update the title. Thanks.