9 Replies Latest reply on May 26, 2017 2:47 AM by Laubender

    Fit Frame to Content with multiple lines of text

    flaming1 Level 2

      Hi

       

      In Indesign CS5 (Mac) is there any way to get the same behaviour that 'Fit Frame to Content' exhibits on a single line of text, to work with multiple lines?

      That is, to fit the frame to the height AND width of the text.

       

      We have black text and the text frame has a fill and a stroke. What we're trying to achieve is to be able to quickly, and accurately, resize the frame for however many lines it runs to. Using the default behaviour we can use 'fit frame to content' to get the height, but we have to manually drag the width.

       

      Thanks

        • 1. Re: Fit Frame to Content with multiple lines of text
          thomas rollins

          i check the overflows property: while (foo.overflows){ var bounds = foo.geometricBounds; foo.geometricBounds = [ bounds[0], bounds[1], bounds[2] + 1, bounds[3] ];  // increase height by a small amount}

           

          though someone else here may be able to suggest a better option!

          • 2. Re: Fit Frame to Content with multiple lines of text
            getZinger Level 1

            Funny question,

             

            Most people want the width to stay the same.

             

            I would suggest something like:

             

            get horizontal offset for each line, push in array, get max value in array, and assign that value to x2

             

             

            var yourDoc = app.activeDocument;

            var yourTextFrame = yourDoc.textFrames[0];

            var myHorOffsetCollection = [];

             

             

            for(myLineIndex=0;myLineIndex<yourTextFrame.parentStory.lines.length;myLineIndex++){

                var myLine = yourTextFrame.parentStory.lines[myLineIndex];

                myHorOffsetCollection.push(myLine.endHorizontalOffset);

            }

             

            var myHighest = Math.max.apply(Math, myHorOffsetCollection);

            var myGB = yourTextFrame.geometricBounds;

            myGB[3] = myHighest;

            yourTextFrame.geometricBounds = myGB;

            1 person found this helpful
            • 3. Re: Fit Frame to Content with multiple lines of text
              Marc Autret Level 4

              Let's try this (CS4/CS5):

               

              // Your Settings:
              var X_PRECISION = .1;     // pts
              
              // Some constants
              var     INNER = CoordinateSpaces.INNER_COORDINATES,
                   MULTIPLY = ResizeMethods.MULTIPLYING_CURRENT_DIMENSIONS_BY,
                   ADDTO = ResizeMethods.ADDING_CURRENT_DIMENSIONS_TO,
                   AP_LEFT = AnchorPoint.TOP_LEFT_ANCHOR,
                   AP_RIGHT = AnchorPoint.TOP_RIGHT_ANCHOR;
              
              function fitHorizontal(/*TextFrame*/ tf, /*str*/xRef)
              //----------------------------------------------------------
              // Fits a textframe width to its content
              // = adjusts the frame width to the optimal value without changing the height
              // xRef (opt.) is the reference pt: 'left'(default) | 'right' | 'center'
              // * You may invoke first tf.fit(FitOptions.FRAME_TO_CONTENT), i.e. fitVertical
              // * This routine supports rotated text frame
              // * Also, unlike the InDesign UI, this supports *multicolumn* TF
              //----------------------------------------------------------
              {
                   // Default width multiplier. This value is only used if
                   // tf overflows in its initial state. 1.5 is fine, usually.
                   var X_FACTOR = 1.5;
                   
                   var ovf = tf.overflows,
                        dx;
              
                   xRef = AnchorPoint['TOP_'+(xRef||'left').toUpperCase()+'_ANCHOR'];
                   
                   // If tf originally overflows, we need to increase the width
                   while( tf.overflows ) tf.resize(INNER,xRef,MULTIPLY,[X_FACTOR,1]);
              
                   // Now, let's compute the maximal width variation (dx)
                   dx = tf.resolve(AP_RIGHT, INNER)[0][0]-tf.resolve(AP_LEFT, INNER)[0][0];
                   if( ovf ) dx *= (1-1/X_FACTOR);
                   
                   // Dichotomy on dx
                   while( dx > X_PRECISION )
                        {
                        dx*=.5;
                        tf.resize(INNER,xRef,ADDTO,[dx*(tf.overflows?1:-1),0]);
                        }
              
                   // Last step, if needed
                   if( tf.overflows ) tf.resize(INNER,xRef,ADDTO,[dx,0]);
              }
              
              //----------------------------------------------------------
              // Sample code
              //----------------------------------------------------------
              
              // Assuming the user has selected a text frame
              var tf = app.selection[0];
              
              // Vertical fit (if you want it first --but not needed)
              tf.fit(FitOptions.FRAME_TO_CONTENT);
              
              // Horizontal fit (from the left edge)
              fitHorizontal(tf, 'left');
              

               

              @+

              Marc

              1 person found this helpful
              • 4. Re: Fit Frame to Content with multiple lines of text
                flaming1 Level 2

                That's perfect thanks Marc!

                 

                It's exactly what we were trying to achieve, so thank you for your solution.

                • 5. Re: Fit Frame to Content with multiple lines of text
                  Harbs. Level 6

                  Marc, Very nicely done!

                   

                  Harbs

                   

                  P.S. (This will work for text frames with basic text. For text frames with footnotes, tables, etc., you would need something much more complex...)

                  • 6. Re: Fit Frame to Content with multiple lines of text
                    bigwillie501

                    this is perfect! just we needed at work... i just have one question, how would i apply this to multiple text boxes?

                     

                    thanks in advance...

                    • 7. Re: Fit Frame to Content with multiple lines of text
                      Level 1

                      This works for one item at a time only, even when more than one frame is selected.

                       

                      Hence, anyway to have it work on all frames that are selected simultaneously?

                       

                      Thanks in advance.

                       

                       

                      Indesign CC 2015 btw.

                       

                      Cheers.

                      • 8. Re: Fit Frame to Content with multiple lines of text
                        Level 1

                        Answered my own inquiry, outside of the script. Apparently Text Frame Options (⌘B) then Auto-Size handled everything perfectly.

                         

                        Thanks again for giving me a starting point at least. Cheers.

                        • 9. Re: Fit Frame to Content with multiple lines of text
                          Laubender Adobe Community Professional & MVP

                          Hi,

                          FWIW: you answered a post from 2011 concerning InDesign CS5.

                          Autosize in Text Frame Options was implemented with CS6.

                           

                          Regards,
                          Uwe