Highlighted

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

Explorer ,
Nov 12, 2020

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

Adobe Community Professional
Correct answer by Stephen_A_Marsh | Adobe Community Professional

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

 

 

TOPICS
Actions and scripting, How to, Make It, Windows

Views

2.3K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

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

Explorer ,
Nov 12, 2020

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

Adobe Community Professional
Correct answer by Stephen_A_Marsh | Adobe Community Professional

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

 

 

TOPICS
Actions and scripting, How to, Make It, Windows

Views

2.3K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Nov 12, 2020 0
Most Valuable Participant ,
Nov 12, 2020

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.

JJMack

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 12, 2020 1
Explorer ,
Nov 14, 2020

Copy link to clipboard

Copied

Thank you JJMack, very good solution.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 14, 2020 0
Adobe Community Professional ,
Nov 15, 2020

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

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 15, 2020 1
Explorer ,
Nov 16, 2020

Copy link to clipboard

Copied

Thank you Stephen_A_Marsh.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Nov 16, 2020 0