Page 1 of 1

PostPosted: Mon Jul 08, 2002 5:38 am
by dewey
Some quick and dirty code for shorten a string to fit in a given width.

<pre>string ellipsedString(string text, int width) {
int prevFont;
int nChars = strlen(text);
Draw draw;
draw.attachForm(mainForm);
draw.begin();
prevFont = draw.font(fntStandard);
// if it already fits or is too short to shorten
// just return the same input
if (nChars < 4 || draw.textWidth(text) < width) {
draw.font(prevFont);
draw.end();
return text;
}

width -= draw.textWidth("...");

while (draw.textWidth(text) > width) {
nChars--;
text = strleft(text, nChars);
}
draw.font(prevFont);
draw.end();
return text + "...";
}</pre>