Skip to main content
Participant
November 12, 2021
Answered

Auto change text size to size of text box?

  • November 12, 2021
  • 10 replies
  • 23338 views

Hi,

Is there a way to make text automatically change size to the size of a text box?

So no matter how much text is entered, it never falls out of the box.

Thanks,

Em

This topic has been closed for replies.
Correct answer hhas01

Touch-wood this should resize text larger/smaller to fit the frame. Caveat emptor, E&OE, etc. If OP requires a more complex script they are welcome to DM me.

 

 

 

 

// fit_text_to_frame.jsx

// resizes text larger/smaller to fit the selected text frame(s)
// (the text frame must be fully selected; point text and non-text items are ignored)
// current limitations: this assumes all characters are same size; leading is not adjusted

(function() {

var MIN_SIZE = 1, MAX_SIZE = 1296 // practical limits, in pts
var MIN_STEP = 0.1 // the smaller this is, the more precise (and slower) the fit

function fitTextToFrame(textFrame) {
    // textFrame : TextFrame -- path or area text
    if (textFrame.typename !== 'TextFrame' || textFrame.kind === TextType.POINTTEXT) { return }
    var length = textFrame.contents.replace(/\s+$/, "").length // length of printable text (ignores trailing whitespace)
    if (length === 0) { return } // skip empty text frame
    // overflow checker; this checks length of printable text to index of last visible character in frame
    var hasOverflow = function() {
        var lastLine = textFrame.lines[textFrame.lines.length-1]
        if (!lastLine) { return true } // no lines visible (frame is too small)
        var visibleLength = (lastLine.characters[0].characterOffset + lastLine.length - 1)
        return visibleLength < length
    }
    // (caution: do not use JS’s +=, -=, *=, /= operators on AI properties as JSX doesn't handle them right)
    var style = textFrame.textRange.characterAttributes
    var size = style.size
    // 1. resize text so it is just overflowing
    while (hasOverflow() && size >= MIN_SIZE * 2) { // decrease size until no overflow
        size /= 2.0
        style.size = size
    }
    if (hasOverflow()) { return } // hit minimum size and it's still overflowing, so give up
    while (!hasOverflow() && size <= MAX_SIZE / 2) { // increase size until text overflows
        size *= 2.0
        style.size = size
    }
    if (!hasOverflow()) { return } // hit maximum size and it hasn't overflowed, so give up
    // 2. now do binary search for the largest size that will just fit the text frame
    var step = size / 2.0
    while (step > MIN_STEP) {
        step /= 2.0
        size += hasOverflow() ? -step : step
        style.size = size
    }
    // 3. if text ends up overflowing after last step, step it back down
    while (hasOverflow()) { 
        style.size = size - step
        step *= 1.1
    }
}

var items = app.activeDocument.selection
for (var i = 0; i < items.length; i++) { // iterate over currently selected items
    fitTextToFrame(items[i])
}

})()

 

 

 

 

10 replies

Participant
May 19, 2023

Can this be applied to an automated action to apply to all the text boxes set on variables? If yes, could you explain to me how?

Kurt Gold
Community Expert
Community Expert
November 20, 2021

That's a very good approach, hhas01. Thank you.

 

I did some tests and observed that for some reason it is sometimes required to execute the script two times in order to get it to work properly.

 

Haven't found a definitive cause that may explain why it happens. But it happens.

 

Perhaps next week I may be able to set up a sample file that contains type objects that do obey the script only when executing it two times.

 

Inspiring
November 21, 2021

I have run some general experiments and noticed a bizarre behavior in Illustrator (25.2.2, 25.3.1, 26.0.1):

 

Create a new document and add a text frame containing text set in 6pt Myriad Pro Regular. Then run the following script:

var textFrame = app.activeDocument.textFrames[0]
textFrame.textRange.characterAttributes.size = 12
alert(textFrame.textRange.characterAttributes.size)

or:

tell application "Adobe Illustrator"
	tell document 1
		set (size of text range of text frame 1) to 12
		get (size of text range of text frame 1)
	end tell
end tell

The text fails to change size and remains at 6pt. Manually set the text to, say, 17.5pt and run the script: it fails to change size again.

 

Try it with a different font style or family, it works. Modify the script to set it to 11.5pt or 14pt, it works.

 

Run the same test in Illustrator 16.0.0 (CS6), it works.

 

If others would like to test and confirm, I would appreciate it.

renél80416020
Inspiring
November 21, 2021

Pour moi

Myriad Pro Regular. ne fonctionne pas sur CS6 16.0.0 64Bits

hhas01Correct answer
Inspiring
November 18, 2021

Touch-wood this should resize text larger/smaller to fit the frame. Caveat emptor, E&OE, etc. If OP requires a more complex script they are welcome to DM me.

 

 

 

 

// fit_text_to_frame.jsx

// resizes text larger/smaller to fit the selected text frame(s)
// (the text frame must be fully selected; point text and non-text items are ignored)
// current limitations: this assumes all characters are same size; leading is not adjusted

(function() {

var MIN_SIZE = 1, MAX_SIZE = 1296 // practical limits, in pts
var MIN_STEP = 0.1 // the smaller this is, the more precise (and slower) the fit

function fitTextToFrame(textFrame) {
    // textFrame : TextFrame -- path or area text
    if (textFrame.typename !== 'TextFrame' || textFrame.kind === TextType.POINTTEXT) { return }
    var length = textFrame.contents.replace(/\s+$/, "").length // length of printable text (ignores trailing whitespace)
    if (length === 0) { return } // skip empty text frame
    // overflow checker; this checks length of printable text to index of last visible character in frame
    var hasOverflow = function() {
        var lastLine = textFrame.lines[textFrame.lines.length-1]
        if (!lastLine) { return true } // no lines visible (frame is too small)
        var visibleLength = (lastLine.characters[0].characterOffset + lastLine.length - 1)
        return visibleLength < length
    }
    // (caution: do not use JS’s +=, -=, *=, /= operators on AI properties as JSX doesn't handle them right)
    var style = textFrame.textRange.characterAttributes
    var size = style.size
    // 1. resize text so it is just overflowing
    while (hasOverflow() && size >= MIN_SIZE * 2) { // decrease size until no overflow
        size /= 2.0
        style.size = size
    }
    if (hasOverflow()) { return } // hit minimum size and it's still overflowing, so give up
    while (!hasOverflow() && size <= MAX_SIZE / 2) { // increase size until text overflows
        size *= 2.0
        style.size = size
    }
    if (!hasOverflow()) { return } // hit maximum size and it hasn't overflowed, so give up
    // 2. now do binary search for the largest size that will just fit the text frame
    var step = size / 2.0
    while (step > MIN_STEP) {
        step /= 2.0
        size += hasOverflow() ? -step : step
        style.size = size
    }
    // 3. if text ends up overflowing after last step, step it back down
    while (hasOverflow()) { 
        style.size = size - step
        step *= 1.1
    }
}

var items = app.activeDocument.selection
for (var i = 0; i < items.length; i++) { // iterate over currently selected items
    fitTextToFrame(items[i])
}

})()

 

 

 

 

Ton Frederiks
Community Expert
Community Expert
November 18, 2021

Very good! I have not been able to find such a script (only one for single lines), but this works fine with a simple test I made.

Inspiring
November 19, 2021

The above script does not care how many lines of text there are, only that they all fit into the text frame. If the text frame is tall enough to accommodate a single short line of large text, and the text frame is set to use auto-leading, it may be that as a longer piece of text is reduced in size it flows onto a second line. If OP wants to avoid that wraparound, the simplest solution would be to set the leading to a fixed value that is greater than the height of the text frame.

 

The kiwi automation engine I built a few years back has a load of options for fitting text and other content (example of capabilities here: [www.youtube.com/watch?v=a8LgiIMpI9s]). The above algorithm is similar to the one used in its `scale text to fit` rule, which has proved reliable, so I’m fairly confident it’ll work without issue. (I’m currently planning a new, cross-platform kiwi system, although whether that ends up being written in JavaScript or something else I’ve yet to decide.)

 

I’ll also update the older `fit_frame_to_text.jsx` script later on, as I noticed a couple minor bugs in it while writing this one.

Kurt Gold
Community Expert
Community Expert
November 14, 2021

Probably or certainly true, hhas.

 

Let's see what further infos emmatomo is going to provide.

 

Kurt Gold
Community Expert
Community Expert
November 14, 2021

So far there are only some long-winded speculations. The entire request can be interpreted in different ways.

 

What is required? At least a sketch that shows the initial situation and the desired ultimate result. Or better yet, some Illustrator sample files.

 

Inspiring
November 14, 2021

OP’s request was clear enough: they want the text within a text frame to resize so that it fits into that frame without overflowing. What’s lacking is sufficient detail to provide a production-ready solution, e.g.:

 

  • Is all the text guaranteed to be the same size or can it be a mix of sizes?
  • Should it step down by fixed amounts (1pt decrements? 5% of the initial size?) or to whatever exactly fits the frame?
  • Are there any particular requirements regarding leading, or can we safely assume it is Auto?
  • Is there a legal minimum size below which text must never go?
  • Should it also increase to fill up excess space or decrease only?
  • Does it need to support linked text frames?
  • Does it need the ability to resize several text frames each by the same amount until none of them overflow?
  • Does it need to be a live effect (expensive!) or is a script that they manually run sufficient?

 

As we’ve both said, if OP can post some sample files it will give us a better idea of what the job entails. If OP’s requirements are minimal, it’s a 5-minute modification to the free fitting script I linked to above, which someone here might do for free if OP can’t code. If it’s more involved, the development work can be budgeted and a proposal put forward.

emmatomoAuthor
Participant
November 15, 2021

Thanks for all of the replies.

 

I probably should have stated my reasoning behind this.

 

I am using varaible data text that varies in length. Not massively, but enough for the text to need to be resized so it fits on one line and within the width of the box.

 

There are no legal requirements regarding the text size.

 

It should both increase or decrease, as I don't have a lower or max limit of characters. I am altering multiple text boxes per page with variable data, though it is only one that needs the auto fit text to size of box.

 

I am not a coder but will take a look at the scripts/plugins suggested so thanks for that. Will also try and post example later 🙂

Ton Frederiks
Community Expert
Community Expert
November 13, 2021

You could do it manually like this:

Use guides to remember the original size of the textbox.

Scale the textbox bounding box with the selection tool and shift key until the overset + sign disappears.

Use the Scale tool to scale the text object back to the original size.

Lukas Engqvist
Community Expert
Community Expert
November 13, 2021

It does sound like it should be possible. But from design perspective it is a bad idea beacuse normally you would want to have control on the typography. You would probably run into legal accessibility issues too. 

Inspiring
November 13, 2021

Sure, but presumably OP has a reason for doing it anyway. (e.g. Multi-language packaging, where text density can vary greatly within a single line depending on whether each localized artwork has to hold 1, 2, or 3 languages. Yes, the resulting artworks may look awful from a design perspective. No, there’s nothing you can do as an artwork grunt as that’s what the customer requires.)

 

As for legal requirements, it’s simple enough to define a minimum font size below which the script will not go. One of the advantages of automated artwork production is that you encode all your brand guidelines, legal requirements, etc within the automation, ensuring consistency and conformance across all artworks.

 

If OP wishes to discuss details they are welcome to post additional information and some examples of the artworks they’re producing.

Met1
Legend
November 12, 2021

Don't start with a text box, just type, and then you can just use the transform panel and change text block to desired width, not sure it's what you want though.

If you want dynamic changing, that's not in Illy.

Again, I'm not sure it's exactly what you mean, but add your voice here:

https://illustrator.uservoice.com/forums/333657-illustrator-desktop-feature-requests/suggestions/37449973-resize-text-to-fit-text-box

You may find something useable here - https://www.linkedin.com/pulse/adobe-illustrator-variable-data-dealing-overset-text-vasily-hall

Kurt Gold
Community Expert
Community Expert
November 12, 2021

Can you show a sketch or a screenshot with a couple of type objects that may illustrate the behaviour you are looking for?

 

You may include further instructions if required.

 

Ton Frederiks
Community Expert
Community Expert
November 12, 2021

Select Auto Size in the Area Type Options.

emmatomoAuthor
Participant
November 12, 2021

Hi Ton,

I have tried this but it doesn't work.

It doesn't change the text size at all, it just contrains text to the width of box unless I am doing something wrong?

Thanks,

Em

Ton Frederiks
Community Expert
Community Expert
November 12, 2021

No, you are not doing something wrong, it was me misunderstanding.

There is no standard option in Illustrator to do this.

Maybe a script?