How to find text width of a TextField in Action Script Flash Professional cs6
note : In action script 2 getTextExtent() method gives width, height, ascent, descent, textFieldHeight, and textFieldWidth of text in the TextField, but in action script 3 to get above things use one of the below methods.
It is very easy to find out width, height, ascent, descent, textFieldHeight, and textFieldWidth of text in the TextField. There are two methods are there
1. Finding width and height of text in the TextField using textWidth and textHeight property of the TextField.
2. Finding width and height of text in the TextField using getLineMetrics() method of the TextField with help of TextLineMetrics class.
Here is the example code above two methods.
package { import flash.display.MovieClip; import flash.text.*; public class TextFieldExample extends MovieClip { var txt_field:TextField= new TextField; var txt_format:TextFormat= new TextFormat; public function TextFieldExample() { // constructor code txt_format.font="Verdana"; txt_format.size=15; txt_format.color=0x00FF00; txt_format.align="center"; txt_field.autoSize = TextFieldAutoSize.LEFT; txt_field.border=true; txt_field.wordWrap=true; txt_field.height=250; txt_field.width=250; txt_field.defaultTextFormat=txt_format; txt_field.text="Keep going, Each step may get harder, but don't stop! The view is beautiful at the top"; trace( 'txt_field.textHeight :'+txt_field.textHeight) ; trace( 'txt_field.textWidth :' +txt_field.textWidth); addChild(txt_field); } } }
Output:
Second Method :
package { import flash.display.MovieClip; import flash.text.*; public class TextFieldExample extends MovieClip { var txt_field:TextField= new TextField; var txt_format:TextFormat= new TextFormat; public function TextFieldExample() { // constructor code txt_format.font="Verdana"; txt_format.size=15; txt_format.color=0x00FF00; txt_format.align="center"; txt_field.autoSize = TextFieldAutoSize.LEFT; txt_field.border=true; txt_field.wordWrap=true; txt_field.height=250; txt_field.width=250; txt_field.defaultTextFormat=txt_format; txt_field.appendText("Keep going, Each step may get harder, but don't stop! The view is beautiful at the top \n"); var txt_metrics:TextLineMetrics = txt_field.getLineMetrics(0); txt_field.appendText("Metrics ascent for the line 1 is: " + txt_metrics.ascent.toString() + "\n"); txt_field.appendText("Metrics descent is: " + txt_metrics.descent.toString() + "\n"); txt_field.appendText("Metrics height is: " + txt_metrics.height.toString() + "\n"); txt_field.appendText("Metrics width is: " + txt_metrics.width.toString() + "\n\n"); addChild(txt_field); } } }
Output :
Download Premium Only Scripts & 80+ Demo scripts Instantly at just 1.95 USD per month + 10% discount to all Exclusive Scripts
If you want any of my script need to be customized according to your business requirement,
Please feel free to contact me [at] muni2explore[at]gmail.com
Note: But it will be charged based on your customization requirement