Jump to content

Add padding to the left based on text length


Meir

Recommended Posts

I have a letterhead I need the address info to be on the top right corner, but aligned left, where the end of the furthest right letter of the Address and/or Address2 lines up with the 8" mark.

 

To compensate for this, I have set a rule that calculates how many spaces it needs to use as padding to before each line. (Feel free to point out a better way.)

 

The below code keeps erroring out on line 52: Error: execution limit exceeded.

 

I'm not sure why it wouldn't work. Any ideas?

 

 

var frameWidth = 2; // set width of the text box (in inches)

// Use TextMeasure to get the length of each company name
var space = new FusionProTextMeasure;
space.pointSize = "8 pt"; // set the type size
space.font = "HelveticaNeue Condensed"; // set your typeface
space.CalculateTextExtent(" ");
space.useTags = false;
var spaceWidth = space.textWidth;

// Use TextMeasure to get the length of each company name
var address1 = new FusionProTextMeasure;
address1.pointSize = "8 pt"; // set the type size
address1.font = "HelveticaNeue Condensed"; // set your typeface
address1.CalculateTextExtent(Field("Address"));
address1.useTags = false;
var addressWidth = address1.textWidth;

// Use TextMeasure to get the length of each company name
var address2 = new FusionProTextMeasure;
address2.pointSize = "8 pt"; // set the type size
address2.font = "HelveticaNeue Condensed"; // set your typeface
address2.CalculateTextExtent(Field("Address2"));
address2.useTags = false;
var address2Width = address2.textWidth;

// Use TextMeasure to get the length of each company name
var gap = new FusionProTextMeasure;
gap.pointSize = "8 pt"; // set the type size
gap.font = "HelveticaNeue Condensed"; // set your typeface
gap.CalculateTextExtent(" • ");
gap.useTags = false;
var gapWidth = gap.textWidth;

var i=1

var success = false
while (success ==  false)
   {
   if (Field("Address2") != "") //if there is an address2
       {
       if((spaceWidth*i) + addressWidth + gapWidth + address2Width >= frameWidth*7200) //if there is an address 2 and the width is greater or equal to frameWidth
           {
               success= true; //then stop looking
           }
       else i= i+1; // if there is an address 2 and the width is less than frameWidth
       };
   if ((spaceWidth*i) + addressWidth >= frameWidth*7200) //if there is no address 2 and the width is greater than frameWidth
       {
       success= true; //then stop looking
       }
   else i= i+1; // if there is no address 2 and the width is less than frameWidth  #### ERRORS OUT ON THIS LINE  ####
   };



var result = Array(i).join('space');

return result;

Edited by Meir
The end code tag did not display.
Link to comment
Share on other sites

I'm not sure what you are trying to return in your result. What is "Array(i)" intended to return?

 

I would build your result first and then measure the entire line at the same time. Then I would offset the line by the difference of the frame width and the text width like so:

// Set variables
var frame = 2; // width of text box in inches
var address = Trim(Field("Address"));
var address2 = Trim(Field("Address2"));

var result = '';
// Add Address to result if it contains a value
if (address != '') result += address;
// add bullet and Address2 if applicable
if (address2 != '' && result != '') result += ' • ' + address2;
// or add Address2 alone if result was previously empty
else if (address2 != '') result += address2;

if (result != '') {
// measure length of result
var txt = new FusionProTextMeasure;
txt.pointSize = "8"; // set the type size
txt.font = "Futura Book"; // set your typeface
txt.CalculateTextExtent(result);

// calculate indent amount
var offset = (frame * 7200) - txt.textWidth;

// add indent to result
   result = '<p findent="' + offset + '">' + result;
}

return result;

Link to comment
Share on other sites

For cleaner code, you could replace all of this:

var address = Trim(Field("Address"));
var address2 = Trim(Field("Address2"));

var result = '';
// Add Address to result if it contains a value
if (address != '') result += address;
// add bullet and Address2 if applicable
if (address2 != '' && result != '') result += ' • ' + address2;
// or add Address2 alone if result was previously empty
else if (address2 != '') result += address2;

 

with this:

var addresses = [Trim(Field("Address"),Trim(Field("Address2")];
var result = addresses.filter(Boolean).join(" • ");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...