Answered
Auto change text size to size of text box?
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
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
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])
}
})()
Already have an account? Login
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.