A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Count number of lines in a text box

  1. #1
    Member
    Join Date
    Jun 2003
    Posts
    40

    Count number of lines in a text box

    Hi, i have a tet boxy dynamically populated from an xml file with html
    I need to know how many lines of text there are so that i can make the text box resize to fit all in

    Thanks

  2. #2
    Senior Member jbum's Avatar
    Join Date
    Feb 2004
    Location
    Los Angeles
    Posts
    2,920
    First of all, if you set the textfield's fld.autoSize (to "center" or "left" or whatever), then the textField's fld.textHeight and fld.textWidth properties will automatically change to fit the text when you modify the fld.text property.

    However, if you still want to measure the number of lines in your text, you can do this:

    Code:
    var nbrLines = -1;
    var charPos = -1;
    var str = fld.text;
    do {
      charPos = str.indexOf("\r",charPos);
      nbrLines++;
    } while (charPos != -1);
    
    // nbrLines now set to correct value
    The following code will also work, and it's shorter, however it is less efficient, because it is splitting the string into an array to make the calculation.

    var nbrLines = fld.text.split("\r").length;

    - Jim

  3. #3
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Or you may need to search for "<br", rather than "\r", depending on the situation.

  4. #4
    Junior Member
    Join Date
    Feb 2004
    Posts
    21
    Your method is flawed jbum.

    charPos = str.indexOf("\r",charPos);

    that should be:

    charPos = str.indexOf("\r",charPos+1);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center