What is the best way to resize a text box to fit the entire text

I have textField (html text) which is very dynamic and I need to resize the window to fix it and calculate the space occupied. I use...

myTextField.height = myTextField.textHeight; 

But the result is not very accurate, even the Adobe description says that it is not. Is there a better way to do this? Is there some kind of "automatic sizing" of this class? And for the latter: what is the purpose of this "textHeight" if it does not provide the real height of the text?

+4
source share
4 answers

textHeight returns the correct value if you specify the width. I also add a margin of 4-6, depending on the text field of the text field, to make sure that all the text is displayed. Although this is somewhat clumsy, it did not fail me with dynamic TFs, but I used the text property, not the htmlText . About automatic resizing is textHeight for this purpose, but there is no actual automatic resizing for text fields.

+2
source

The TextField class has a getLineMetrics () method that returns TextLineMetrics .

There are interesting examples on both pages.

You can get the number of text lines in a multiline text field using the numLines () method.

enter image description here

+7
source

do the following:

 myTextField.autoSize = TextFieldAutoSize.LEFT; // if it should not be singleline then add this aswell myTextField.wordWrap = true; myTextField.multiline = true; 

Regarding your second question:
textHeight returns the height of the text in pixels.
check example in docs -> AS3 Docs TextField.textHeight

+3
source

If you need something more precise, I found a solution from viewing docs , 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.

+1
source

Source: https://habr.com/ru/post/1484536/


All Articles