TextLineMetrics Tutorial
♥2 |
Line 38 |
Modified 2009-09-26 15:12:25 |
MIT License
archived:2017-03-04 19:37:16
ActionScript3 source code
/**
* Copyright 9re ( http://wonderfl.net/user/9re )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ppQJ
*/
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init();"
paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5">
<mx:Script>
<![CDATA[
import flash.events.Event;
import flash.text.TextLineMetrics;
import flash.utils.clearTimeout;
import flash.utils.setTimeout;
private const INIT_TEXT:String = "getLineMetrics () method\npublic function getLineMetrics(lineIndex:int):flash.text:TextLineMetrics\n\nReturns a TextLineMetrics object with information about the text position and measurements for a line of text in the control. The component must be validated to get a correct number. If you set the text or htmlText property and then immediately call getLineMetrics() you may receive an incorrect value. You should either wait for the component to validate or call validateNow(). This is behavior differs from that of the flash.text.TextField class, which updates the value immediately.\n\nParameters\n lineIndex:int — The zero-based index of the line for which to get the metrics.\n\nReturns\n flash.text:TextLineMetrics — The object that contains information about the text position and measurements for the specified line of text in the control.\n\nSee also\nflash.text.TextField\nflash.text.TextLineMetrics\n";
private var _logTimerID:int;
private function init():void {
textArea.text = INIT_TEXT;
textArea.dispatchEvent(new Event(Event.CHANGE));
logArea.editable = false;
}
private function onChange(e:Event):void {
clearTimeout(_logTimerID);
_logTimerID = setTimeout(logLineMetrics, 1000);
}
private function logLineMetrics():void {
var i:int = 0;
var tlm:TextLineMetrics;
logArea.text = "";
while (true) {
try {
tlm = textArea.getLineMetrics(i);
} catch (e:Error) {
break;
}
logArea.text += "line: " + i++ + " width: " + tlm.width + " height: " + tlm.height + "\n";
}
}
]]>
</mx:Script>
<mx:TextArea id="textArea" width="100%" height="50%" change="onChange(event);" />
<mx:TextArea id="logArea" width="100%" height="50%" />
</mx:Application>