Jump to content

Floating Images


Fletch

Recommended Posts

I have a business card that has a possibility of 1, 2 or 3 phone numbers on it. They are stacked ontop of each other. The each have a corresponding image to the left of the number (Cell image, Phone image and Fax image) When there are less than 3 numbers, the correct image needs to appear next to the corresponding number, and the empty spaces need to disappear. The baseline for the numbers will be centered from top to bottom within the text box. I've created image rules to place the images, but I do not know how to have them float. I know how to do this with text, but not images. What's the best solution?

I uploaded a file named 'phone_number.jpg' which shows the layout.

Link to comment
Share on other sites

One way to do this would be to use inline graphics in a text rule:

// Number logos:
var cellImg = '/path/to/cell.png';
var phoneImg = '/path/to/phone.png';
var faxImg = '/path/to/fax.png';

// Number offset from baseline
var offset = 50;

var numbers = [
   [cellImg, Field('cell')],
   [phoneImg, Field('phone')],
   [faxImg, Field('fax')]
   ].filter(function(s){return s[1]}).map(function(s){return CreateResource(s[0],'graphic').content + '<p br=false superoffset=' + offset + ' superratio=100><superscript>' + s[1] + '</superscript>';}).join('<br>')

return numbers;

Note that the 'offset' variable is just used to superscript the actual phone number in order to give it the appearance that it's vertically aligned to the middle of the icons.

Link to comment
Share on other sites

Hi Ste

 

Thats awesome. I have almost the exact same template to build. My text is to the left of the icons thou. I can assume I just need to rearrange the code to have the "Field" called out first and then the image, correct?

As for the first three lines, i can just create then as a graphic resource and call then out as such, correct?

 

Thanks Stu - You guys are awesome

Link to comment
Share on other sites

Hi Ste

 

Thats awesome. I have almost the exact same template to build. My text is to the left of the icons thou. I can assume I just need to rearrange the code to have the "Field" called out first and then the image, correct?

Actually, all you'd have to change is the map function:

// Number logos:
var cellImg = '/path/to/cell.png';
var phoneImg = '/path/to/phone.png';
var faxImg = '/path/to/fax.png';

// Number offset from baseline
var offset = 50;

var numbers = [
   [cellImg, Field('cell')],
   [phoneImg, Field('phone')],
   [faxImg, Field('fax')]
   ].filter(function(s){return s[1]}).map(
       function(s){
           [color="Red"]return '<p br=false superoffset=' + offset + ' superratio=100><superscript>' + s[1] + '</superscript>' + 
               CreateResource(s[0],'graphic').content;[/color]
   }).join('<br>')

return numbers;

Within that function is where all of the "magic" is happening. That's where the image path is being converted to a resource and where the inner array ([img, number]) is being converted to a string where 's[0]' is the img and 's[1]' is the number.

As for the first three lines, i can just create then as a graphic resource and call then out as such, correct?

Sure you could create them as graphic resources, but you'd have to make a few changes to account for that:

// Number logos:
[color="red"]var cellImg = Resource("cell").content;
var phoneImg = Resource("phone").content;
var faxImg = Resource("fax").content;[/color]

// Number offset from baseline
var offset = 50;

var numbers = [
   [cellImg, Field('cell')],
   [phoneImg, Field('phone')],
   [faxImg, Field('fax')]
   ].filter(function(s){return s[1]}).map(
       function(s){
           [color="red"]return '<p br=false superoffset=' + offset + ' superratio=100><superscript>' + s[1] + '</superscript>' + s[0];[/color]
   }).join('<br>')

return numbers;

Link to comment
Share on other sites

Oh, I'm just noticing that you're running FusionPro 7 which uses JavaScript 1.5. The 'filter' and 'map' functions were introduced in JavaScript 1.6. You could insert Array.prototype.filter and Array.prototype.map to the beginning of the previously mentioned code or you can just change the code to this:

// Number logos:
var cellImg = Resource("cell").content;
var phoneImg = Resource("phone").content;
var faxImg = Resource("fax").content;

// Number offset from baseline
var offset = 50;

var result = [];
var numbers = [[cellImg, Field('cell')],[phoneImg, Field('phone')],[faxImg, Field('fax')]];

for (var i in numbers)
   if (numbers[i][1])
       result.push(numbers[i][0] + '<p br=false superoffset=' + offset + ' superratio=100><superscript>' + numbers[i][1] + '</superscript>');

return result.join('<br>');

Link to comment
Share on other sites

That definately got rid of that error. When I validate, it says expression ok. However, when I preview, all I see is the a bunch of text from the rule. I have that one rule placed in a varible text box. Should I have done something else? see attached screen shot.
Link to comment
Share on other sites

Taht was it. Thank you. Please take a look at the attached jpg file. The images are being cut-off on the left and the bottom. I've tried changing all the settings in each of the resources, as well as within the text box, but nothing is affecting the images. I double-checked the images. They are vector from Illustrator. Is the problem that they are circles?
Link to comment
Share on other sites

You could add space to the right side of the graphic itself, you could add a non-breaking space ( ), or you could add a tab and set the spacing by adjusting your tab-stops.

 

Here's an example of adding a non-breaking space:

// Number logos:
var cellImg = Resource("cell").content;
var phoneImg = Resource("phone").content;
var faxImg = Resource("fax").content;

// Number offset from baseline
var offset = 50;

var result = [];
var numbers = [[cellImg, Field('cell')],[phoneImg, Field('phone')],[faxImg, Field('fax')]];

for (var i in numbers)
   if (numbers[i][1])
       result.push(numbers[i][0] + '<p br=false superoffset=' + offset + ' superratio=100><superscript>[color="Red"] [/color]' + numbers[i][1] + '</superscript>');

return result.join('<br>');

Link to comment
Share on other sites

Hi STE

 

Thanks again for your help. I needed to change the order of the Icons and add some space, but was able to figure it out on my own with the code your provided. One last question. If I wanted to force formatting for the number so they always appeared as 123.456.7891 i could just copy and paste some of the code from the prebuilt rules, but where in the coding would i put it.

 

Thanks again

Link to comment
Share on other sites

You can make 3 separate phone formatting rules for each number and call them like this:

// Number logos:
var cellImg = Resource("cell").content;
var phoneImg = Resource("phone").content;
var faxImg = Resource("fax").content;

// Number offset from baseline
var offset = 50;

var result = [];
var numbers = [[cellImg, [color="Red"]Rule('cell')[/color]],[phoneImg, [color="red"]Rule('phone')[/color]],[faxImg, [color="red"]Rule('fax')[/color]]];

for (var i in numbers)
   if (numbers[i][1])
       result.push(numbers[i][0] + '<p br=false superoffset=' + offset + ' superratio=100><superscript> ' + numbers[i][1] + '</superscript>');

return result.join('<br>');

 

Or you could do create a function to format the numbers and do this:

// Number logos:
var cellImg = Resource("cell").content;
var phoneImg = Resource("phone").content;
var faxImg = Resource("fax").content;

// Number offset from baseline
var offset = 50;

var result = [];
var numbers = [[cellImg, Field('cell')],[phoneImg, Field('phone')],[faxImg, Field('fax')]];

for (var i in numbers)
   if (numbers[i][1])
       result.push(numbers[i][0] + '<p br=false superoffset=' + offset + ' superratio=100><superscript> ' + [color="red"]formatNumber(numbers[i][1])[/color] + '</superscript>');

return result.join('<br>');


// Format Phone Number 
[color="red"]function formatNumber(number){
   var number = Trim(number);
   var pattern01 = /^(\d{3})[^\d]*(\d{4})$/;   						
   var pattern02 = /^[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})$/;
   var pattern03 = /^\+?(\d{1})[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})$/;
   var pattern04 = /^[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})\D*[x#n]\D*(\d+)$/;
   var pattern05 = /^\+?(\d{1})[\D]*(\d{3})[\D]*(\d{3})[\D]*(\d{4})\D*[x#n]\D*(\d+)$/;
   var pattern06 = /^(\d{3})[\D]*(\d{4})\D*[x#n]\D*(\d+)$/;
   var patternEndExt = /(.)[x#n](.)/;
   var patternStart1 = /^[\D]*[1]/;

   if(number.match(pattern01)){
       number = number.replace(pattern01, "$1.$2");
       return number;
   } else if(number.match(pattern02)){
       number = number.replace(pattern02, "$1.$2.$3");
       return number;
   } else if(number.match(pattern03)){
       if (number.match(patternStart1)){
           number = number.replace(pattern03, "+$1 $2.$3.$4");
           return number;
       } else {
           return number;
       }
   } else if(number.match(pattern04)){
       number = number.replace(pattern04, "$1.$2.$3 ext.$4");
       return number; 
   } else if(number.match(pattern05)){
       number = number.replace(pattern05, "+$1 $2.$3.$4 ext.$5");
       return number;
   }  else if(number.match(pattern06)){
       number = number.replace(pattern06, "$1.$2 ext.$3");
       return number;
   } else {
       //return "no match any pattern";
       return number;
   }	
}[/color]

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