PDA

View Full Version : Changing Fonts for Numbers


traba5058
October 24th, 2012, 08:12 AM
I've been using Marcomm Central & Fusion Pro since Feb, so I am still fairly new at javascript.

I am setting up a new business card template for Marcomm Central using FusionPro and need help. This client uses one font for text and one font for numbers. This means the number font needs to be used for phone & mobile numbers as well as the zip code & numbers in the address 1 & address 2 fields. FusionPro makes it very easy to assign a font to a field, so I can easily assign the font to the phone & mobile numbers as well as the zip code field. I'm having problems with address1 & address2 fields.

Is there javascript or a setting that will look for numbers in a field and use a font? Please help.

esmith
October 24th, 2012, 08:46 AM
Create the following text rule:
var address = your address field;
address = address.replace(/(\d+)/g,"<f name='number_font'>$1</f>");
return address;
(Alternatively, you could build your entire address block (street, city, state, zip, etc) for your variable.)

Place the rule in a text frame and apply the text font you want. The code will replace all number sequences in your variable string with a tagged version of same string(s) using the number font you add to the code. Be sure to check 'Treat returned strings as tagged text' in the rule. :)

traba5058
October 24th, 2012, 09:03 AM
Thanks Eric!
I updated your rule to read
var address = Field("Street 1");
address = address.replace(/(\d+)/g,"<f name='Sebastion Light UCF'>$1</f>");
return address;

and

var address = Field("Street 2");
address = address.replace(/(\d+)/g,"<f name='Sebastion Light UCF'>$1</f>");
return address;

I also checked the treat tagged text box.

The preview is returning a different font for the numbers in the field but it looks like a default font or something. It doesn't match the font in the phone & mobile number fields or the zip code field. I entered the name of the font as it appears in the drop down in FusionPro. Is there a different way it needs to be entered?

Dan Korn
October 24th, 2012, 09:08 AM
You need double-quotes in the markup tags. Try this instead:
return Field("Street 2").replace(/(\d+)/g,'<f name="Sebastion Light UCF">$1</f>');Also, you might not have the correct font name. Do a regular composition (not a Preview) and look in the composition log (.msg) file for any font warnings.

You can always determine the correct font name to use in an <f> tag by creating a Formatted Text resource, entering some text in the desired font, then exiting the Text Editor and clicking "View Source." That will show you the correct <f name=***> tag.

dreimer
October 24th, 2012, 09:13 AM
Both rules work for me but I have a problem when the street name is a number. For example 123 86th Ave is returned 12386th Ave.

esmith
October 24th, 2012, 09:13 AM
Your updates look correct to me. When I google the font you are using it "corrects" the name to be "Sebastian Light UCF" -- are you sure the name isn't misspelled?

If that is not the issue, you may want to temporarily try another (more common) font in the tag to see if the problem is with the specific font or something broader. Also, if you reverse the font assignments (for testing only) does "Sebastion Light UCF" preview correctly for the alpha characters?

esmith
October 24th, 2012, 09:23 AM
Both rules work for me but I have a problem when the street name is a number. For example 123 86th Ave is returned 12386th Ave.
Good catch dreimer! The code vallidates correctly with a space in that scenario but does not appear to format it correctly in the preview. I tweaked my original code and changed the single/double quotes that Dan suggested (which I was unaware of):

var address = '123 21South Main Street, Suite 24A 3 SW 1342A5692';
address = address.replace(/\s/g,"&nbsp;").replace(/(\d+)/g,'<f name="FolioSS-Bold">$1</f>');
return address;

dreimer
October 24th, 2012, 09:28 AM
Perfect, the new code worked correctly.

Getting great stuff off this forum of late, thank you everyone!!!

Dan Korn
October 24th, 2012, 09:39 AM
Good catch dreimer! The code vallidates correctly with a space in that scenario but does not appear to format it correctly in the preview. I tweaked my original code and changed the single/double quotes that Dan suggested (which I was unaware of):

var address = '123 21South Main Street, Suite 24A 3 SW 1342A5692';
address = address.replace(/\s/g,"&nbsp;").replace(/(\d+)/g,'<f name="FolioSS-Bold">$1</f>');
return address;
That will work, except that you're replacing every space with a non-breaking space, which will prevent the address from wrapping to multiple lines properly. If that's not an issue in your job, then it's fine. If, however, you do want to maintain the breaking spaces, then you need to do something like this instead:
function replace_helper(match, digits, spaces)
{
return '<f name="Courier New">' + digits + '</f>' +
Array(spaces.length + 1).join(" ");
}

return Field("Address").replace(/(\d+)(\s*)/g, replace_helper);More info: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter

traba5058
October 24th, 2012, 10:09 AM
Thanks to All! It was the quotes & spelling of Sebastian. I'm using:
var address = Field("Street 1");
address = address.replace(/\s/g,"&nbsp;").replace(/(\d+)/g,'<f name="Sebastian Light UCF">$1</f>');
return address;
and
var address = Field("Street 2");
address = address.replace(/\s/g,"&nbsp;").replace(/(\d+)/g,'<f name="Sebastian Light UCF">$1</f>');
return address;

Can you tell me what you mean by breaking? I have copyfit on and hyphenation off & do not break on copyfit on. I do not want address 1 to move to the next line & I do not want address 2 to move to the next line. I want copy to shrink to fit in address block. Do I have to worry about breaking?

Dan Korn
October 24th, 2012, 10:44 AM
Can you tell me what you mean by breaking? I have copyfit on and hyphenation off & do not break on copyfit on. I do not want address 1 to move to the next line & I do not want address 2 to move to the next line. I want copy to shrink to fit in address block. Do I have to worry about breaking?
No, if you don't want the address to break to multiple lines, then what you have should be fine.