Setting the Width of a Field via Lingo

Date:    Tue, 21 Jan 1997 23:51:26 +0000
From:    Andrew White 
Subject: Re: Setting the width of a field

>I'm writing a script that automatically creates a new #field cast member
>and puts several lines of text into it. I then want to display it on the
>stage.
>
>My problem is I end up with the field being the default width so I'm
>trying to work out how to examine the contents of the field (they
>change!) and dynamically set the width of the field accordingly.
>
>I actually want the white background of the field to be seen in this
>case so I can't just 'fudge' it and set the ink to background
>transparent either.
>
>Any ideas?

First the easy part... You can use the rect of member property to size a text field.

Now the not so easy part ;)

You need to find the length of the longest line in the text field:

on calcFieldSize theField

  -- Get some important values and zero out our variables

  set lineCount = the number of lines in field theField
  set charCount = the number of chars in field theField
  set maxWidth = 0
  set theHeight = 0

  -- Get the locH of each character and compare with our
  -- previous max locH, keeping the larger value

  repeat with i = 1 to charCount
    set theLocH = getAt(charPosToLoc(member theField,i),1)
    set maxWidth = max(maxWidth,theLocH)
  end repeat

  -- Get the line height for each line and sum them
  -- If your lines are all the same height you
  -- could just use lineCount * lineHeight(member theField, 1)

  repeat with i = 1 to lineCount
    set theHeight = theHeight + lineheight(member theField, i)
  end repeat

  -- set the rect of the field using your newly calculated values

  set the rect of member theField to rect(0,0,maxWidth,theHeight)

end
Just a couple of notes - in the cast info box set the field type to fixed with no word wrap.

URL: