Photoshop Scripting:创建一个选择,然后在里面放置一个文本框

问题描述 投票:0回答:1

使用Javascript for Photoshop(ExtendScript)我正在尝试制作一个动态文本框。

我的最终目标是一个脚本,用户将自己的文本写入弹出的新窗口,然后该文本位于图像的底部10%,动态缩放以适应图像的底部10%。

因此,无论低10%的维度(由resizeCanvas参数创建),文本总是适合它。因此,当用户添加几个单词时,它看起来不会太小,当他们添加更长的句子时,它不会溢出。

问题最初在这里问(带图片)https://forums.adobe.com/message/10952562#10952562

示例开始图像:

start image

窗口弹出:

custom PopUp

小文字结果:

outcome1

长篇文章结果:

outcome 2

这就是我目前所处的位置:

#target Photoshop  
  
app.preferences.rulerUnits = Units.PIXELS;  
app.preferences.typeUnits = TypeUnits.PIXELS;  
var doc = app.activeDocument;  
var hgt10 = doc.height / 10;  
var FillColor = new SolidColor;  
FillColor.rgb.hexValue = 'ff0000';  
var newHgt = doc.height + hgt10  
doc.resizeCanvas(doc.width, newHgt, AnchorPosition.TOPCENTER);  
  
  var win = new Window('dialog', "Custom Text");  
  var grp1 = win.add('group');  
  grp1.alignChildren = "top";  
  var txt = grp1.add('edittext', [0, 0, 300, 150], "Write your message", {multiline: true, scrolling: true});  
  txt.active = true;  
  var grp2 = win.add('group');  
  var goBtn = grp2.add('button', undefined, "Run");  
  grp2.add('button', undefined, "Cancel");  
    
  var newLyr = doc.artLayers.add();  
  newLyr.kind = LayerKind.TEXT;  
  // CONTENTS  
  var txtLyr = newLyr.textItem  
  txtLyr.font = "ArialMT"  
  txtLyr.contents = txt.text  
  var uV = new UnitValue(20,'mm')  
  txtLyr.size = UnitValue(25,'mm')  
  txtLyr.color = FillColor;  
  txtLyr.position = [25, 2200] // x, y - Need to fix position to fit in the lower 10%  
  
  goBtn.onClick = function() {  
    win.close();   
  }  
  
win.show();     

所以我想要一些帮助的领域是:

1)使文本位于新区域(由resizeCanvas参数创建,延长10%)

2)添加换行符并自动调整大小以使各种长度的文本文件适合字段。

亲切的问候,

javascript adobe photoshop extendscript photoshop-script
1个回答
0
投票

要添加换行符,请用\ns替换所有\rs(至少对于Windows,我觉得这个Mac版本可能需要其他东西,但我不确定):

txtLyr.contents = txt.text.replace(/\n/gm, '\r')

要定位文本,您需要知道大小(这取决于行号)和前导。

var lines = txt.text.split('\n'); //getting the number of lines
var maxSize = hgt10 / lines.length; //calculating max size for a line (including the leading) = 10% of doc height devided by lines number
newLyr.textItem.useAutoLeading = true // making sure that text uses autoleading
var leading = newLyr.textItem.autoLeadingAmount; //autoleading value is in % of text size
maxSize = maxSize / leading * 100; // recalculating text size based on leading in pixels
txtLyr.size = maxSize;
txtLyr.position = [25, originalDocHeight + maxSize]

结果有一行和几行:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.