Making an ellipsed string...

Discuss and publish sample code and applications written in OrbC (OrbForms Designer or PocketC Architect)

Postby dewey on Mon Jul 08, 2002 5:38 am

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>
dewey
Site Admin
 
Posts: 749
Joined: Sun Oct 21, 2007 5:12 am

Return to OrbC User Samples

Who is online

Users browsing this forum: No registered users and 1 guest

cron