如何通过javascript缩小文本以使其适合Illustrator中的给定矩形?

时间:2018-11-28 11:05:24

标签: javascript adobe-illustrator

我在Illustrator中通过javascript创建了一个areaText,并希望以最大可能的大小将文本放入给定框中。该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以创建一个链接到初始areaText的溢出区域,并缩小大小,直到该区域中没有更多文本为止。

示例:

var docRef = documents.add(null,1080,1080);
var piRef = activeDocument.pathItems;
var textRefs = activeDocument.textFrames;

var pathRef = piRef.add();

// target text area
var textArea = piRef.rectangle(940, 140, 800, 600);
var text = textRefs.areaText(textArea);

// maximum Font size, use something big to shrink down from
var maxFontSize = 200

text.textRange.characterAttributes.size = maxFontSize;

// create an overflow box
var overflowArea = piRef.rectangle(940, 140, 800, 600);
var overflowText = textRefs.areaText(overflowArea, TextOrientation.HORIZONTAL, text);

text.contents = "This text should fit in the box.";

// shrink text size until the overflow box is empty.
var fontSize = maxFontSize;
while (overflowText.words.length > 0 && fontSize > 0)
{
    text.textRange.characterAttributes.size = --fontSize;
    overflowText.textRange.characterAttributes.size = --fontSize;
}