-
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
-
Senior Member
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
-
Or you may need to search for "<br", rather than "\r", depending on the situation.
-
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|