Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So, let me get this straight, you have N text fields of differing widths and you want to remove 1 or more of them, rearranging the remaining M text fields, redistributing their widths proportionally (i.e. so if text field T<sub>1</sub> was twice the width of text field T<sub>2</sub>, it would still be after they are rearranged)?</p> <p>This isn't actually a very simple problem, but I've taken a stab at it below. You pass it the array of your text fields (from left to right), the number you want to keep, and an optional X-offset.</p> <p><strong>Note:</strong> I haven't done AS3 in months so consider the below to be pseudocode--you will have to edit it to make it work.</p> <pre><code>function arrangeTextFields(textFields:Array, keep:int, xOffset:Number = 0) { // we can't keep more fields than are provided keep = Math.min(keep, textFields.length); var totalWidth = 0, // the total width of all textfields keptOrigWidth = 0, // the total combined widths of the ones you're keeping origNum:int = textFields.length; // calculate the above values by addition in a loop // (because AS3 lacks an inject/reduce method) for(var i:int, i++, i &lt; origNum) { totalWidth += textFields[i].width; if(i &lt; take) { keptOrigWidth += textFields[i].width; } } var ratio:Number = totalWidth / takenOrigWidth, // the ratio we'll resize each field by to keep them proportional currentX:int = 0; textFields = textFields.slice(0, keep - 1); // throw away the fields we're not keeping for(int i = 0; i &lt; take; i++) { textFields[i].width = textFields[i].width * ratio; // resize it textFields[i].x = currentX + xOffset; // move it currentX += textFields[i].width; } } </code></pre> <p><strong>Note:</strong> This code doesn't actually remove the "unkept" text fields from view; you'll have to add that or do it elsewhere.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload