3 Replies Latest reply on Nov 29, 2009 12:23 PM by -LokkiOnLintu-

    Making text fit the first line of a text frame

    -LokkiOnLintu-

      When importing xml data into a text frame I would like to make it so that the imported text would be formatted to always fit the first row of the text frame.

       

      The xml data I import are just product names but there is still a possibility of the name being too long to fit one line if I only have one font size for the text frame.

       

      Hmm...I hope I explained it clearly enough.

       

       

      Any suggestions/solutions are appreciated.

       

      I use Javascript and InDesign CS3

        • 1. Re: Making text fit the first line of a text frame
          [Jongware] Most Valuable Participant

          This script does that, on a selected text frame.

           

          if (app.selection.length != 1)
          complain ("Eh. You oughta have one (and only one) text frame selected...");
          theFrame = app.selection[0];
          if (theFrame.constructor.name != "TextFrame")
          complain ("Honestly, this only works on a text frame...");
          shrinkMe (theFrame);
          
          function shrinkMe (frame)
          {
          while (frame.texts.everyItem().pointSize > 10 && frame.paragraphs[0].lines.length > 1)
          frame.texts.everyItem().pointSize -= 10;
          frame.texts.everyItem().pointSize += 10;
          while (frame.texts.everyItem().pointSize > 1 && frame.paragraphs[0].lines.length > 1)
          frame.texts.everyItem().pointSize -= 1;
          frame.texts.everyItem().pointSize += 1;
          while (frame.texts.everyItem().pointSize > 0.1 && frame.paragraphs[0].lines.length > 1)
          frame.texts.everyItem().pointSize -= 0.1;
          while (frame.texts.everyItem().pointSize > 0.01 && frame.paragraphs[0].lines.length > 1)
          frame.texts.everyItem().pointSize -= 0.01;
          }
          

           

          Not visible in my Character formatting palette, but apparently ID can work with hundreds of a point for text size -- the text run is clearly different between 8.01 and 8.02 pts!

           

          Insert the "shrinkMe" call in your import function -- be sure to feed it a text frame. (From within an XML item, it probably needs a parent, or perhaps parentStory.parent -- something like that.)

          • 2. Re: Making text fit the first line of a text frame
            [Jongware] Most Valuable Participant

            Hm. Just in case you need every item to fit onto a single line, use this instead:

             

            if (app.selection.length != 1)
             complain ("Eh. You oughta have one (and only one) text frame selected...");
            theFrame = app.selection[0];
            if (theFrame.constructor.name != "TextFrame")
             complain ("Honestly, this only works on a text frame...");
            ShrinkAllOfMe (theFrame);
            function ShrinkAllOfMe (frame)
            {
             // Set Ridiculously Small size:
             frame.texts.everyItem().pointSize = 0.1;
             lines = frame.lines.length;
             while (frame.lines.length == lines)
              frame.texts.everyItem().pointSize += 10;
             frame.texts.everyItem().pointSize -= 10;
             while (frame.lines.length == lines)
              frame.texts.everyItem().pointSize += 1;
             frame.texts.everyItem().pointSize -= 1;
             while (frame.lines.length == lines)
              frame.texts.everyItem().pointSize += 0.1;
             frame.texts.everyItem().pointSize -= 0.1;
             while (frame.lines.length == lines)
              frame.texts.everyItem().pointSize += 0.01;
             frame.texts.everyItem().pointSize -= 0.01;
            }
            

             

            You probably should make sure the text does not overflow -- I have no idea what happens then. ("Sanity Warning: Endless Loop Imminent!" ?).

            1 person found this helpful
            • 3. Re: Making text fit the first line of a text frame
              -LokkiOnLintu- Level 1

              Thank you very much.

              That was really helpful.