6 Replies Latest reply on Oct 8, 2017 11:14 PM by c.pfaffenbichler

    How to measure/scale text to fit desired width?

    suncodr

      I would like to measure the width of a given word or words (not a paragraph) when rendered with a given font and font size. Let's say I have the code below that creates a new document, layer, and text:

       

      var myDoc = app.documents.add(200, 200);
      
      
      var textLayer = myDoc.artLayers.add();
      textLayer.kind = LayerKind.TEXT;
      
      var text = textLayer.textItem;
      text.font = "Impact";
      text.size = 72;
      text.position = Array(100,100);
      text.contents = "SUPER";
      

       

      I want to measure the width of text.

       

      Ultimately I want to resize the text to be a certain width while maintaining its aspect ratio. (The height of the text is not important, just the width, and it should not be distorted.) Knowing the width I could easily calculate the correct font size. If there is a better way to this solution than measuring the width, please let me know.

       

      Thanks!