'''// 创建UI面板
var dialog = new Window('dialog', '生成多个文本图层');
dialog.orientation = 'column';
// 折叠和展开按钮组
var collapseExpandGroup = dialog.add('group');
collapseExpandGroup.orientation = 'row';
var expandButton = collapseExpandGroup.add('button', undefined, '展开');
var collapseButton = collapseExpandGroup.add('button', undefined, '折叠');
// 输入框组:每行内容
var inputGroup = dialog.add('group');
inputGroup.orientation = 'column';
inputGroup.add('statictext', undefined, '请输入每行内容:');
var inputField = inputGroup.add('edittext', [0, 0, 300, 150], '', {multiline: true});
inputField.characters = 30;
inputField.minimumSize = [300, 150];
// 输入框组:最大行数
var rowGroup = dialog.add('group');
rowGroup.orientation = 'row';
rowGroup.add('statictext', undefined, '最大行数:');
var maxRowField = rowGroup.add('edittext', undefined, '10');
maxRowField.characters = 5;
// 输入框组:字体大小
var fontSizeGroup = dialog.add('group');
fontSizeGroup.orientation = 'row';
fontSizeGroup.add('statictext', undefined, '字体大小:');
var fontSizeField = fontSizeGroup.add('edittext', undefined, '20');
fontSizeField.characters = 5;
// 输入框组:字体颜色(RGB)
var colorGroup = dialog.add('group');
colorGroup.orientation = 'row';
colorGroup.add('statictext', undefined, '字体颜色 (RGB):');
var redField = colorGroup.add('edittext', undefined, '0');
redField.characters = 3;
var greenField = colorGroup.add('edittext', undefined, '0');
greenField.characters = 3;
var blueField = colorGroup.add('edittext', undefined, '0');
blueField.characters = 3;
// 输入框组:列宽
// var colSizeGroup = dialog.add('group');
// colSizeGroup.orientation = 'row';
// colSizeGroup.add('statictext', undefined, '列宽大小:');
// var colSizeField = colSizeGroup.add('edittext', undefined, '200');
// colSizeField.characters = 5;
// 按钮组:确定和取消按钮
var buttonGroup = dialog.add('group');
buttonGroup.orientation = 'row';
var okButton = buttonGroup.add('button', undefined, '生成文本图层');
var cancelButton = buttonGroup.add('button', undefined, '取消');
// 声明展开高度变量
var expandedHeight;
// 展开按钮点击事件
expandButton.onClick = function() {
dialog.size.height = 500;
};
// 折叠按钮点击事件
collapseButton.onClick = function() {
dialog.size.height = collapseExpandGroup.size.height + buttonGroup.size.height;
};
okButton.onClick = function() {
try {
// 获取输入框内容
var lines = inputField.text.split('\n');
var maxRow = parseInt(maxRowField.text, 10);
var fontSize = parseInt(fontSizeField.text, 10);
var red = parseInt(redField.text, 10);
var green = parseInt(greenField.text, 10);
var blue = parseInt(blueField.text, 10);
// var colSize = parseInt(colSizeField.text, 10);
var colSize = fontSize*4.1;
// 创建文本图层
var doc = app.activeDocument;
createTextLayers(lines, maxRow, fontSize, [red, green, blue], colSize);
// 强制刷新文档显示
doc.activeLayer = doc.activeLayer; // 触发重绘
app.refresh();
// 可选:自动选择第一个生成的图层以便编辑
if(doc.artLayers.length > 0) {
doc.activeLayer = doc.artLayers[doc.artLayers.length - 1];
}
// 保持对话框打开,但给予用户视觉反馈
alert("文本图层已生成!\n你可以继续输入内容并再次生成。");
} catch (e) {
alert("生成文本图层时出错: " + e);
}
};
var startRow = 1; // 起始行
var startCol = 0;
function createTextLayers(lines, maxRow, fontSize, fontColor, colSize) {
const textlist = lines;
const fontFamily = "Adobe 黑体 Std"; // 字体
const ColsSpacing = (colSize / 72) * 2.54;
const rowsSpacing = (fontSize / 72) * 2.54; // 间距计算(基于字体大小)
for (var i = 0; i < textlist.length; i++) {
var content = textlist[i];
var position = [startCol * ColsSpacing, startRow * rowsSpacing];
if (startRow % maxRow == 0) {
startRow = 1;
startCol++;
} else {
startRow++;
}
createTextLayer(content, position, fontSize, fontColor, fontFamily);
}
}
function createTextLayer(content, position, size, color, font) {
var doc = app.activeDocument;
// 创建新文本图层
var textLayer = doc.artLayers.add();
textLayer.kind = LayerKind.TEXT;
textLayer.name = content;
// 设置文本内容
var textItem = textLayer.textItem;
textItem.contents = content;
// 设置文本位置
textItem.position = [position[0], position[1]];
// 设置文本大小
textItem.size = size;
// 设置文本颜色
var rgbColor = new SolidColor();
rgbColor.rgb.red = color[0];
rgbColor.rgb.green = color[1];
rgbColor.rgb.blue = color[2];
textItem.color = rgbColor;
// 设置字体
try {
textItem.font = font;
} catch (e) {
// 忽略字体设置错误
}
}
// 取消按钮点击事件
cancelButton.onClick = function() {
dialog.close();
};
// 显示UI
dialog.center();
dialog.show();
try{
// dialog.size.height = titleBar.size.height + buttonGroup.size.height + resizeHandle.size.height + 10;
dialog.size.height =50;
}
catch(e){
dialog.close();
}
// 在对话框显示后获取展开高度
// expandedHeight = dialog.size.height;
// dialog.size.height = expandedHeight;
// 默认折叠窗口 - 修复:将这行移到获取展开高度之后"""
为什么我的jsx代码运行之后,点击生成文本图层之后,它不会每行往下排,并且满行之后向右一列呢
最新发布