johper Posted February 5, 2016 Posted February 5, 2016 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. Quote
step Posted February 5, 2016 Posted February 5, 2016 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; } Quote
johper Posted February 8, 2016 Author Posted February 8, 2016 Thank you very much! Works perfectly! Quote
Recommended Posts
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.