Jump to content

Changing Fonts for Characters


traba5058

Recommended Posts

I currently have 3 rules to change the numbers in the specific field to a different font. These rules work.

 

RuleAddressFont

return Field("Address").replace(/(\d+)/g,'<f name="Gotham Medium">$1</f>');

 

RuleFaxFont

return Rule("RuleFaxFormat").replace(/(\d+)/g,'<f name="Gotham Medium">$1</f>');

 

RulePhoneFont

return Rule("RulePhoneFormat").replace(/(\d+)/g,'<f name="Gotham Medium">$1</f>');

 

I need assistance with the following:

 

1. Including change in font size & leading for the numbers: Font size with Gotham Medium should be 7pt with 8.4pt leading

 

2. Including ( ) and - characters in the phone & fax rules

 

If someone can use the phone or fax rule above to show me how to do this, I can then apply it to the others.

 

All help will be much appreciated.

Edited by traba5058
To clarify & simplify and hopefully get a response
Link to comment
Share on other sites

Right now, you're capturing all numbers and assigning them to the '$1' variable which you then wrap in your font tag. If you want to expand that regexp to include parentheses and hyphens, I think this would work for you:

 

return Field("Address").replace(/([\(\)\-\d]+)/g, '<span font="Gotham Medium">$1</span>');

 

Here's a more in depth break-down of how it works:

var find = new RegExp(
 '(' +   // Parenthesis CAPTURE matched patterns and assign them to $1
 '[' +   // Square brackets denote a character set that the RegExp should match
 '\\(' + // First character to match: ( <-- Open parenthesis
         // Note that we escape so we don't start another capture group
 '\\)' + // Second character to match: ) <-- Close parenthesis
 '\\-' + // Third character to match: - <-- hyphen
 '\\d' + // All digits 0-9
 ']' +   // Close the square brackets because we only want to match what's inside
 '+' +   // Plus sign means we want to match 1 or more of the characters in the []
 ')'     // Close parenthesis to end capturing
,'g');   // g means "Match globally"

// Each captured match is assigned to $1 so we can wrap it in font tags 
var replace = '<span font="Gotham Medium">$1</span>';

return Field("Address").replace(find,replace);

Link to comment
Share on other sites

Step's solution seems fine, other than not addressing the point size change, although I think this will work as well:

return Field("Address").replace(/([\d\W]+)/g, '<span font="Gotham Medium" pointsize=7>$1</span>');

The \W means "any non-word character", basically anything that's not a letter or digit, and \d means a digit, and the square brackets mean "match anything in here", so [\d\W] means basically anything that's not a letter. For reference:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions

 

As I noted in the other thread, you shouldn't really need to do anything special in the rule for leading. By default, you're automatically going to get 8.4 point leading for a line where the largest text is 7 point. If you need to adjust this, you can change the auto-leading factor in the Paragraph Formatting dialog.

 

Also, if you want, you can put this logic into a function in the JavaScript Globals like so:

function FormatAddressOrPhone(text)
{
   return String(text).replace(/([\d\W]+)/g, '<span font="Gotham Medium" pointsize=7>$1</f>');
}

Or name it whatever you want. Then instead of every rule needing to have all that special code in it, you can just have each rule call the function, like so:

return FormatAddressOrPhone(Field("Address"));

and:

return FormatAddressOrPhone(Rule("RuleFaxFormat"));

etc.

Edited by Dan Korn
corrected ending span tag
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...