13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Using the TextField class<br />

public function getOptimalHeight(str:String):int<br />

{<br />

if (field.text == "" || field.text == null)<br />

{<br />

return this.preferredHeight;<br />

}<br />

else<br />

{<br />

this.linesPerCol = Math.ceil(field.numLines / this.numColumns);<br />

}<br />

}<br />

var metrics:TextLineMetrics = field.getLineMetrics(0);<br />

this.lineHeight = metrics.height;<br />

var prefHeight:int = linesPerCol * this.lineHeight;<br />

return prefHeight + 4;<br />

First the getOptimalHeight() method calculates the width of each column. Th<strong>en</strong> it sets the width and htmlText<br />

property of the first TextField object in the array. The getOptimalHeight() method uses that first TextField object<br />

to discover the total number of word-wrapped lines in the text, and from that it id<strong>en</strong>tifies how many lines should be<br />

in each column. Next it calls the TextField.getLineMetrics() method to retrieve a TextLineMetrics object that<br />

contains details about size of the text in the first line. The TextLineMetrics.height property repres<strong>en</strong>ts the full<br />

height of a line of text, in pixels, including the asc<strong>en</strong>t, desc<strong>en</strong>t, and leading. The optimal height for the<br />

MultiColumnTextField object is th<strong>en</strong> the line height multiplied by the number of lines per column, plus 4 to account<br />

for the two-pixel border at the top and the bottom of a TextField object.<br />

Here is the code for the full layoutColumns() method:<br />

public function layoutColumns():void<br />

{<br />

if (this._text == "" || this._text == null)<br />

{<br />

return;<br />

}<br />

var field:TextField = fieldArray[0] as TextField;<br />

field.text = this._text;<br />

field.setTextFormat(this.format);<br />

this.preferredHeight = this.getOptimalHeight(field);<br />

var remainder:String = this._text;<br />

var fieldText:String = "";<br />

var lastLineEndedPara:Boolean = true;<br />

var ind<strong>en</strong>t:Number = this.format.ind<strong>en</strong>t as Number;<br />

for (var i:int = 0; i < fieldArray.l<strong>en</strong>gth; i++)<br />

{<br />

field = this.fieldArray[i] as TextField;<br />

field.height = this.preferredHeight;<br />

field.text = remainder;<br />

field.setTextFormat(this.format);<br />

Last updated 6/6/2012<br />

394

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!