TextField multiline + wrap get text height

I have a TextField with multiline and word wrap enabled and fixed width enabled. I want to calculate the total height of the text inside it.

I tried using TextField.textHeight , but that gives me the height of one line. Due to the wrapper, I cannot easily calculate the number of lines to multiply it by the height of the line. TextField.height just gives me the default fixed height for the margin, 100 pixels.

So how to do this?

+4
source share
3 answers

There should be an error in your code, since textHeight should return the height of the TextField, and not just one line height.

+4
source

make sure you include wordWrap

these traces are txt.textHeight = 135

  var format: TextFormat = new TextFormat ();
 format.font = new Bauhaus () .fontName;

 var txt: TextField = new TextField ();
 txt.embedFonts = true;
 txt.multiline = true;
 txt.defaultTextFormat = format;
 txt.wordWrap = true;
 txt.width = 100;
 txt.text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi metus diam, condimentum sagittis rutrum vitae, vehicula et velit.";
 addChild (txt);

 trace ("txt.textHeight" + txt.textHeight); 
+1
source

I found a solution by looking at Documents , and this tip .

Both of them work great for me with single-line multi-line hyphenation (only for text hyphenation and \ n) and multi-line hyphenation (just \ n).

Short penultimate version (do not use this):

 var totalLines = textField.bottomScrollV - textField.scrollV + textField.maxScrollV; var metrics = textField.getLineMetrics(0); var gutter = 2; var actualHeight = (metrics.ascent + metrics.descent) * totalLines + (totalLines - 1) * metrics.leading + 2 * gutter; 

A longer version where the rows have different metrics (use this):

 var gutter = 2; var totalLines = textField.bottomScrollV - textField.scrollV + textField.maxScrollV; var actualHeight = 0; var prevLeading = 0; for (var i = 0; i < totalLines; i += 1) { var metrics = textField.getLineMetrics(i); actualHeight += metrics.ascent + metrics.descent + prevLeading; prevLeading = metrics.leading; } actualHeight += 2 * gutter; 

For one test, lines with an embedded image where textField height gives me 32, textHeight gives me 39, the calculated height (actualHeight above) is 34. For a multi-line test where the height is 97.20, textHeight is 23.79, actualHeight 97.15. This actual height includes a gutter on both sides, but removes the final lead, if any.

0
source

All Articles