Skip to main content
mimozemská hůl
Inspiring
November 12, 2020
Answered

Trying to do text to fit the width of the canvas dimensions

  • November 12, 2020
  • 2 replies
  • 2911 views

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

This topic has been closed for replies.
Correct answer Stephen Marsh

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;
}

 

 

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
November 15, 2020

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;
}

 

 

mimozemská hůl
Inspiring
November 16, 2020

Thank you Stephen_A_Marsh.

JJMack
Community Expert
Community Expert
November 12, 2020

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.

JJMack
mimozemská hůl
Inspiring
November 14, 2020

Thank you JJMack, very good solution.