Copy link to clipboard
Copied
Hi,
The scipt below creates text layer puts a custom text and then it centers. I need the size of the text to fit the width of the canvas dimensions. Please, how can I do that?
Here is the script:
activeDocument.suspendHistory('CreateCenterTxt', 'CreateCenterTxt()');
function CreateCenterTxt() {
var doc = activeDocument;
var ArtLrs = doc.artLayers.add();
ArtLrs.kind = LayerKind.TEXT;
var TxtRef = ArtLrs.textItem;
TxtRef.contents = "Some Text";
TxtRef.size = 20; // Text must fit the width of the canvas
var docW = Number(doc.width);
var docH = Number(doc.height);
var layer = doc.activeLayer;
var bounds = layer.bounds;
var layerW = Number(bounds[2] - bounds[0]);
var layerH = Number(bounds[3] - bounds[1]);
var dX = (docW - layerW) / 2 - Number(bounds[0]);
var dY = (docH - layerH) / 2 - Number(bounds[1]);
layer.translate(dX, dY);
}
Thanks,
Damian
I believe text is based on points like 72 ppi. I believe I need to save the documents print resolution. Then set the document resolution to 72ppi. I then used the Canvas width in pixel to calculate the font sized I needed to spread my text across the canvas,. Knowing the longest text line in number of characters, I divided the document's width in pixels by number of character in the longest like. I used the result as the font size added the text layers. Then restored the document P
...I would have "cheated":
/* Original code - https://forums.adobe.com/thread/988084 */
// Resize Active Layer to Doc Width.jsx
#target photoshop
resizeToDocWidth();
function resizeToDocWidth() {
if (!documents.length) return;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
// var res = doc.resolution;
var dWidth = doc.width;
var LB = activeDocument.activeLayer.bounds;
var Width = LB[2
...
Copy link to clipboard
Copied
I believe text is based on points like 72 ppi. I believe I need to save the documents print resolution. Then set the document resolution to 72ppi. I then used the Canvas width in pixel to calculate the font sized I needed to spread my text across the canvas,. Knowing the longest text line in number of characters, I divided the document's width in pixels by number of character in the longest like. I used the result as the font size added the text layers. Then restored the document Print resolution.
Copy link to clipboard
Copied
Thank you JJMack, very good solution.
Copy link to clipboard
Copied
I would have "cheated":
/* Original code - https://forums.adobe.com/thread/988084 */
// Resize Active Layer to Doc Width.jsx
#target photoshop
resizeToDocWidth();
function resizeToDocWidth() {
if (!documents.length) return;
var startRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;
var doc = app.activeDocument;
// var res = doc.resolution;
var dWidth = doc.width;
var LB = activeDocument.activeLayer.bounds;
var Width = LB[2].value - LB[0].value;
var onePix = 100 / Width;
var newSize = onePix * dWidth;
doc.activeLayer.resize(newSize, newSize, AnchorPosition.MIDDLECENTER);
app.preferences.rulerUnits = startRulerUnits;
}
Copy link to clipboard
Copied
Thank you Stephen_A_Marsh.