Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Explorer ,
Nov 12, 2020 Nov 12, 2020

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

TOPICS
Actions and scripting , Windows
2.9K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Nov 12, 2020 Nov 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 P

...
Translate
Community Expert , Nov 15, 2020 Nov 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
...
Translate
Adobe
Community Expert ,
Nov 12, 2020 Nov 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 14, 2020 Nov 14, 2020

Thank you JJMack, very good solution.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 15, 2020 Nov 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;
}

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 16, 2020 Nov 16, 2020
LATEST

Thank you Stephen_A_Marsh.

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