4 Replies Latest reply on Feb 25, 2012 5:21 AM by jugenjury

    Conditional Font Sizing for a Text Overlay on Photo

    apmaui808 Level 1

      The photo size is 640 pixels x 800 pixels. I can get the script working at 9 points font, but not if the name overlayed is too long without resizing the font.

       

      Here are the main settings of the initial script I prepared.

       

       

      var originalRulerUnits = preferences.rulerUnits;

      preferences.rulerUnits = Units.PERCENT;

       

      var docRef = activeDocument;

       

      var myLayerRef = docRef.artLayers.add();

      myLayerRef.kind = LayerKind.TEXT;

      myLayerRef.name = "Filename";

      var myTextRef = myLayerRef.textItem;

       

      // Set font size in Points (may change depending on character-length)

      myTextRef.size = 9;

       

      myTextRef.font = "Arial";

       

      // Text color in RGB values

      var newColor = new SolidColor();

      newColor.rgb.red = 255;

      newColor.rgb.green = 255;

      newColor.rgb.blue = 255;

      myTextRef.color = newColor;

       

      // Set the position of the text

      myTextRef.position = new Array( 50, 96.875);

      myTextRef.justification = Justification.CENTER    

       

      // Set the Blend Mode of the Text Layer

      myLayerRef.blendMode = BlendMode.NORMAL;

       

      // Opacity in percentage

      myLayerRef.opacity = 100;

       

       

      The text length is based on a variable "fname" (concatenating first name and last name from a database) which is written and overlayed at the bottom part of the photo has to be only one line.

       

      The example above shows 9 points as the default font size.

       

      Here are the conditions, which I don't know how to make:

       

      1. If the character count is less than 40, then use 9 points font;

       

      2. If the character count is 40 to 50, then use 8 points font;

       

      3. If the characted count is 51 and over, then use 7 points font.

       

      Thanks in advanced.

        • 1. Re: Conditional Font Sizing for a Text Overlay on Photo
          c.pfaffenbichler Level 9

          You could try a switch clause:

           

          switch (true) {
                    case (myTextRef.contents.length < 40): 
                    myTextRef.size = 9;
                    break;
                    case ((myTextRef.contents.length >= 40) && (myTextRef.contents.length <= 50)): 
                    myTextRef.size = 8;
                    break;
                    default:
                    myTextRef.size = 7;
                    break;
                    };
          
          • 2. Re: Conditional Font Sizing for a Text Overlay on Photo
            c.pfaffenbichler Level 9

            I think xbytor posted some code a while ago he uses in a contact sheet Script to check if text runs longer than expected (and intended in the layout) and correct type properties accordingly.

            If you can expect to have texts that will overshoot the width even at 7pt you might try looking for that.

             

            Another option might be to convert the text layer to a Smart Object, evaluate the layer’s bounds and scale it down if necessary.

            • 3. Re: Conditional Font Sizing for a Text Overlay on Photo
              JJMack Most Valuable Participant

              Scripts

              I have always found text hard to size.  In a script I normaly save the documents resolution  change it to 72 the calulate some size based on the number of pixels wide the document is factored by the number of characters I want on a line.  I  add the Text then restore the documents resolution.

               

              Actions

              When I record an action what I do while recording the action I use my script that save and restores  a documents resolution. then in the action I change the Image size to a known width like 8" without resampling this only changes the documents resolution. I add the text layer with a font size approprate to the documents 8" width. Then use the script a second time to restore the document to its original size.

              • 4. Re: Conditional Font Sizing for a Text Overlay on Photo
                jugenjury Level 2

                My normal method would be to type out the text in the largest font I'd ever use then transform to resize it to fit within the bounds required. This can give you the ability to have the text display across a specific percentage of the image no matter how many characters there are.