Contracting text to a fixed width

Terry Judd tsj at unimelb.edu.au
Thu Feb 22 00:18:35 EST 2007


I'm looking for a way to contract unwrapped lines of text to a fixed width -
primarily for use in a table where the width of columns is
variable/adjustable and often narrower than the text each 'cell' contains. I
reckon a trailing ellipsis added to the contracted text would look
satisfactory and managed to come up with the following routine that uses an
intermediate field...

function contractTextToWidth theText,theWidth
  lock screen
  put "..." into fld "CTTW"
  put the formattedWidth of char 1 to 3 of fld "CTTW" into tWidthA
  -- tWidthA = width of trailing ellipsis
  repeat with i = length(theText) down to 1
    put theText into fld "CTTW"
    put the formattedWidth of char 1 to i of fld "CTTW" into tWidthB
    -- tWidthB = width of remaining string
    if tWidthA+tWidthB <= theWidth then
      return theText&"..."
      exit repeat
    end if
    delete last char of theText
  end repeat
  unlock screen
end contractTextToWidth

... But I'm thinking there's probably a better way (particularly when lots
of cells are involved - I might end up with a couple of hundred). Any ideas?

Terry...




More information about the Use-livecode mailing list