Truncate Text Based On Available Space?

J. Landman Gay jacque at hyperactivesw.com
Wed Jun 12 22:47:22 EDT 2013


On 6/12/13 8:14 PM, Scott Rossi wrote:
> Has anyone created a routine that will truncate text in a field to given
> pixel width, and append ellipses?
>
> I found one that limits text based on character count, but I'm looking to
> limit based on available space.

I did this ages ago, like back in MC. I used a real field, but now that 
I know better I'd use the templatefield instead. It's ugly, but since 
you can only measure text width from a field, you have to loop. The 
pseudocode is something like:

store the original text somewhere for reference later
put it into a field
repeat until the formattedwidth of the text is <= the column:
   delete last char of the field
   put "..." into last char of the field
end repeat

Even with a "real" field it was still pretty fast. Probably would be 
faster with the templatefield. The "..." isn't three periods, it needs 
to be the real elipsis character so that the "last" character includes 
all three visual dots.

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the use-livecode mailing list