• Global community
    • Language:
      • Deutsch
      • English
      • EspaƱol
      • FranƧais
      • PortuguĆŖs
  • ę—„ęœ¬čŖžć‚³ćƒŸćƒ„ćƒ‹ćƒ†ć‚£
    Dedicated community for Japanese speakers
  • ķ•œźµ­ ģ»¤ė®¤ė‹ˆķ‹°
    Dedicated community for Korean speakers
Exit
0

Count lines in a paragraph

New Here ,
Jan 12, 2015 Jan 12, 2015

Copy link to clipboard

Copied

I'm finally stacked with the last portion of the script and have to ask you guys.

Task:

I'm reading 2 bits of information from the text file, title and description. And placing those to the document, one after another.

Problem:

I need to place description right after title, having small N pixels margin. Originally title has one string, but I iterate this process and read from different text files, so some titles are in 2 strings, and I have no idea how to find out where to place description. Here is how it should look.

example.png

And here is my code:

function createTexts() {
  createText
(250, 100, 110, titles[count_psd-1], 1);
  createText
(200, 50, 250, descriptions[count_psd-1], 0);

  saveImage
();
}

function createText(h, fontSize, hPosition, content, light) {
  
var doc = app.activeDocument;
  
var TextLayer = doc.artLayers.add();
  
TextLayer.kind = LayerKind.TEXT;
  
var txtRef = TextLayer.textItem;
  txtRef
.kind = TextType.PARAGRAPHTEXT;
  txtRef
.antiAliasMethod = AntiAlias.SMOOTH;
  txtRef
.width = 1050;
  txtRef
.height = h;
  
if (lang==0) {
  
if (light==1){
  txtRef
.font = "HelveticaNeue-UltraLight";
  
} else {
  txtRef
.font = "HelveticaNeue";
  
}
  
} else {
  txtRef
.font = "MicrosoftSansSerif";
  
}
  txtRef
.contents = content;
  txtRef
.size = fontSize;
  
var textPosition = [75,hPosition];
  txtRef
.position = textPosition;
}

So basically any other solution that will solve my problem will work.

Thanks in advance!

UPD: update the code slightly to have it more appealing.

TOPICS
Actions and scripting

Views

267

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
community guidelines
Adobe
Participant ,
Jan 26, 2015 Jan 26, 2015

Copy link to clipboard

Copied

You can't know the number of lines in a paragraph,  but you can guess...

var text1 = 'Very very longest title ever ever ever';

var text2 = 'Description';

// The maximum number of characters per line

// Depends on the selected font and font size

var maxChar = 20;

function createTexts() {

  var titleHeight = createText(125, 100, 110, text1);

  createText(100, 50, titleHeight+100, text2);

}

function createText(h, fontSize, hPosition, content) {

   var doc = app.activeDocument;

   var TextLayer = doc.artLayers.add();

   TextLayer.kind = LayerKind.TEXT;

   var txtRef = TextLayer.textItem;

  txtRef.kind = TextType.PARAGRAPHTEXT;

  txtRef.antiAliasMethod = AntiAlias.SMOOTH;

  txtRef.width = 1050;

  txtRef.contents = content;

  txtRef.size = fontSize;

  var textPosition = [75,hPosition];

  txtRef.position = textPosition;

  var text = txtRef.contents;

  //==========================

  var ost = content.length % maxChar;

  var k = (content.length - ost) / maxChar;

  txtRef.height = h*(k+1);

  return txtRef.height;

}

createTexts();

Votes

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
community guidelines
Enthusiast ,
Jan 26, 2015 Jan 26, 2015

Copy link to clipboard

Copied

LATEST

Taking above even further, you could first add just one characters and establish base line height estimate if you can't have it static. You could even keep adding chars until you get a wrap to estimate both line height and line space.

height_after_wrap = 2*original_height+line_space  <=> line_space = height_after_wrap -  2*original_height

Votes

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
community guidelines