Skip to main content
Known Participant
February 11, 2022
Question

Counting the number of lines in a text field using JavaScript

  • February 11, 2022
  • 2 replies
  • 4133 views

Hi

Using JavaScript how can I count the number of lines in a text field including lines created as a result of wrapped text.

    This topic has been closed for replies.

    2 replies

    try67
    Adobe Expert
    February 12, 2022

    There's no built-in way to do it. You will need to write a custom function to calculate the length of the text based on the font, font size and the characters used in it.

    SamKhaMAuthor
    Known Participant
    February 12, 2022

    Could you please provide me with a sample code that calculates the length of the text based on the font, font size and the characters used in it. I am using Font "Calibri" size 11

    try67
    Adobe Expert
    February 12, 2022

    No, you will need to write it yourself, or hire someone to do it for you.

    JoãoCésar17023019
    Inspiring
    February 12, 2022

    Hi.

     

    Have you tried the getMetrics method?

     

    For example:

     

    console.log(this.yourTextField.getMetrics().lines.length);

     

     

    Please let us know.

     

    Regards,

    JC

    SamKhaMAuthor
    Known Participant
    February 12, 2022

    I did not try this function before. Please could your proivide with a sample script using this fuction to call from a Button. I tried the following code and it did not work.

     

    var txtField=this.getField("Labels");

    app.alert(txtField.getMetrics().lines.length);

     

    I received the following error:

     

    TypeError: txtField.getMetrices is not function

    JoãoCésar17023019
    Inspiring
    February 12, 2022

    Of course.

     

    Here it is:

    var _this = this;
    
    _this.yourButton.on("click", function()
    {
    	console.log(_this.yourTextField.getMetrics().lines.length);
    });