Jump to content

Recommended Posts

Posted

Hi,

 

I'm creating a business card template and I have a problem getting rid of of a separator at the end of a text line when there's a line break. The customer can select different brands in a html checkbox list and automatically the selected brands align with comma between them. I have managed to replace the comma with dash and no break space and allow only linebreak after the dash. But I also want to remove the dash at the end of the line in case there is a line break.

 

Image: http://s30.postimg.org/ajtqvu94h/procar.jpg

 

 

This is code I´m using so far:

 

 

var mystring = (Field("Extra line"))

 

function NoBreak(s)

{

return s.replace(/ /g, " ");

}

mystring2 = NoBreak(mystring);

 

var newchar = ' - '

mystring3 = mystring2.split(',').join(newchar);

 

return mystring3;

 

 

 

Any help would be appreciated.

procar.jpg.4bbb09a1ba6c13a146d6cde013d6ba9c.jpg

Posted

I think the only way to do this is to use TextMeasure. You'd have to check the width of the string as it relates to the width of the text frame for each item that you add. If the item causes the string to be greater than the width of the text frame, add it with a page break; otherwise, add it with a dash:

/*
// Make sure that "Treat returned strings as tagged text" and 
// "Re-evaluate this rule for every text flow" is checked. 
// Make sure that you have given the frame a name
*/

// Width of the frame. Set to 3 inches when validating the rule.
var frameWidth = !FusionPro.inValidation ? GetSettableTextWidth(FindTextFrame(FusionPro.Composition.CurrentFlow.name)) : 21600;
var str = Field('Extra Line').split(',');                // Create an array of cars by splitting on a comma (,)
var result = '';                                         // String to hold the result
while (str.length) {
   var cat = [result,Trim(str.shift())].filter(String); // Array to hold concatenated string
   var glue = ' - ';                                    // Join the new car to the result string with a dash (-) by default
   if (getTextWidth(cat.join(glue)) > frameWidth)       // If joining with a dash exceeds the width of the frame, 
       glue = '<br>';                                   // join with a line break instead
   result = cat.join(glue);                             // Set the result to the concatenated string
}
return result;


/*
// Helper function that determines the width of a string ('input')
*/
function getTextWidth(input){
   var tm = new FusionProTextMeasure;
   tm.pointSize = "12pt";  // Font size of the string we're returning
   tm.font = "Helvetica";  // Font face of the string we're returning
   tm.CalculateTextExtent(input);
   return tm.textWidth;
}

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...