Jump to content

Adjusting Leading when text wraps


-Lisa-

Recommended Posts

Can anyone help me tie up the loose ends here?

 

I have a field with 9pt leading applied, however, if that field wraps to a second line, the second line (and any subsequent lines) need to be set to 7pts. I'm sure I need to somehow combine the leading tag with the the FusionProTextMeasure object but I'm still a JavaScript "newbie" (yes, even after all this time) so I don't even know how to begin this one.

 

Anyone up for giving me a head start? :D

 

Thanks!

Link to comment
Share on other sites

If I understand what you're asking, you're trying to have a list of items that have 9pt leading applied to them but if one of those items has to wrap to another line, you want to tighten up the leading on it so that it's not confused for a separate item?

 

If that's the case, I think you can do this by measuring the item, field, variable (or whatever it may be) using text measure to see if it is wider than the frame. If it's not, return it with 9pt leading, if it is, in order to keep the items equally spaced, you want to keep the leading of the first character of that item as 9pt and change everything else to 7pt in order to tighten up the rest of it.

 

I think it would look something like this:

/*========================================
//
// Edit these variables
//
//=======================================*/
   var field = Field("NAME"); // Your variable
   var frameWidth = GetSettableTextWidth(FindTextFrame("YOUR FRAME NAME")); // 100th's of a point 1in = 7200

   // Object of settings to be passed to the formatText function
   var settings = {
       font: "Helvetica",
       fontSize: 9, // in points
       defaultLeading: 12, // in points
       altLeading: 6 // in points
       }

   return formatText(field, frameWidth, settings);


/*========================================
//
// Function adds alternate leading after 
// the second character in the string if
// it determines it will wrap
//
//=======================================*/
   function formatText(input, width, config){
       // Assign to variables & set defaults
       var config = (config) ? config : new Object();
       var fontFace = config.font || "Helvetica";
       var pt = config.fontSize || 12;
       var lead = (config.defaultLeading*10) || (pt*12);
       var altLead = (config.altLeading*10) || (lead*0.75);

       // Tagged the text for TextMeasure
       var tags = '<span font="' + fontFace + '" pointsize=' + pt + '><leading newsize="'+lead+'">';
       var result = tags + input + '</leading></span>';

       // Initiate TextMeasure & Get Width
       var tm = new FusionProTextMeasure;
       tm.useTags = true;
       tm.CalculateTextExtent(result);

       // If Text is longer than the frame, adjust leading
       if (tm.textWidth > width){
           result = result.replace(new RegExp('(' + tags + '.)',""),
                     '$1</leading><leading newsize=' + altLead + '>');
       }

       return result;
   }

 

Just remember that if you're going to hard-code the width of the text frame, it needs to be in 100th's of a point in order to be accurate. I hope this helps!

Edited by step
Link to comment
Share on other sites

Thank you both!

 

David, unfortunately your method did not work. It actually returns the opposite result from what I'm trying to achieve. The first line leading needs to be set to 11pts. If the text from this field wraps to a second line, the second line's leading should be set to 8pts.

 

Ste, I appreciate the full commented code you provided! Unfortunately, I'm receiving an error that "result is not defined". I have a feeling it's got to be because I have not hard-coded my width. If I'm using the GetSettableTextWidth function then should the width already be calculated?

 

Thanks for the help!

Link to comment
Share on other sites

Well, I don't think that not hard-coding the width should case an issue since the code I posted doesn't use a hard-coded width.

 

The error that "result" is not defined, leads me to believe something weird happened when you copied that "formatText" function into your template (since that's where the 'result' variable is used). Could you post your rule? Or even more helpful, collect your template and upload it to this forum so I could have a better idea of what you're working with?

Link to comment
Share on other sites

Thanks Ste. Once again you prove to be a valuable asset to these forums!!

 

I reviewed the code last night and was able to locate the error when I copied the rule over so I no longer receive the "result" error. However, when placing the rule into my template, it is now only returning the first letter of the field.

 

I am unable to post the template to the boards but I created a "test" template showing you the size of the text frame I'm working with and with the rule inserted.

 

Another related question:

Will this rule still function properly if this becomes a multi-line text field and the user forces a line break?

 

Thank you!!!

Test_Template.zip

Link to comment
Share on other sites

It looks like the closing '>' on line 34 got converted to a '.' in your rule. If you check to make sure the if statement in the function looks like this (edit in red):

//If Text is longer than the frame, adjust leading 
if (tm.textWidth > width){
   result = result.replace(new RegExp('(' + tags + '.)',""),
       '$1</leading><leading newsize=' + altLead + '[color="Red"]>[/color]');

}

you should see more than just the first character of the title.

 

Will this rule still function properly if this becomes a multi-line text field and the user forces a line break?

In order to ensure that the leading is adjusted for forced-line breaks ("<br>") you could alter the function to adjust the leading when the text is wider than the width of the text frame OR if the field contains a break tag (in red):

function formatText(input, width, config){
   //Assign to variables and set defaults
   var config = (config) ? config : new Object();
   var fontFace = config.font || "Times New Roman Italic";
   var pt = config.fontSize || 7;
   var lead = (config.defaultLeading*10) || (pt*12);
   var altLead = (config.altLeading*10) || (lead*0.75);

   //Tag the text for TextMeasure
   var tags = '<span font="' + fontFace + '" pointsize=' + pt + '><leading newsize="'+lead+'">';
   var result = tags + input + '</leading></span>';

   //Initate TextMeasure & GetWidth
   var tm = new FusionProTextMeasure;
   tm.useTags = true;
   tm.CalculateTextExtent(result);

   [color="red"]var useAltLeading = ((tm.textWidth > width) || (input.search(/<br>/g) > -1));[/color]

   //If Text is longer than the frame, adjust leading 
   if ([color="red"]useAltLeading[/color]){
       result = result.replace(new RegExp('(' + tags + '.)',""),
           '$1</leading><leading newsize=' + altLead + '>');

   }

   return result;
}

Link to comment
Share on other sites

Thanks Ste! Much appreciated! I fixed the code which corrected the one-character issue and works when the line wraps...but forcing a line break just reverts everything back to the auto-leading. I'll keep playing around with this. Thank you SO much for the feedback and the assistance!!
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...