TextEngine Sample
...
@author ...
package
{
import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
import flash.text.engine.ContentElement;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.GraphicElement;
import flash.text.engine.TextBlock;
import flash.text.engine.TextElement;
import flash.text.engine.GroupElement;
import flash.text.engine.TextLine;
import flash.text.engine.FontWeight;
import flash.text.engine.FontPosture;
import flash.text.engine.FontLookup;
import flash.text.TextFormat;
/**
* ...
* @author ...
*/
public class TextEngineSample001 extends Sprite
{
public function TextEngineSample001()
{
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(e:Event):void
{
drawGrid(new Rectangle(0, 0, stage.stageWidth, stage.stageHeight), 15);
var fontName:String = "Arial Bold";
var fontDesc:FontDescription = new FontDescription(fontName);
var elementFormat:ElementFormat = new ElementFormat(fontDesc, 40, 0xA2DE65);
var sprite:Sprite = new Sprite();
sprite.graphics.beginFill(0xDD669E, 1);
sprite.graphics.drawRoundRect(0, 0, 150, 100, 20);
sprite.graphics.endFill();
sprite.addEventListener(MouseEvent.CLICK, clickHandler);
var graphicElement:GraphicElement
= new GraphicElement(sprite, sprite.width, sprite.height, elementFormat, sprite);
var textElement:TextElement = new TextElement("Hello, Flash Text Engine!", elementFormat);
var elements:Vector.<ContentElement> = Vector.<ContentElement>([graphicElement, textElement]);
var groupElement:GroupElement = new GroupElement(elements, elementFormat);
var textBlock:TextBlock = new TextBlock();
textBlock.content = groupElement;
var textLine:TextLine = textBlock.createTextLine(null, 465);
textLine.x = 10;
textLine.y = 200;
addChild(textLine);
}
private function clickHandler(e:MouseEvent):void
{
trace("clickHandler");
}
private function drawGrid(rect:Rectangle, size:Number):void {
trace("draw grid");
var i:int;
var g:Graphics = this.graphics;
g.lineStyle(1, 0x53D9F0);
i = rect.y;
while (i < rect.height) {
g.moveTo(0, i);
g.lineTo(rect.width, i);
i += size;
}
i = rect.x;
while (i < rect.width) {
g.moveTo(i, 0);
g.lineTo(i, rect.height);
i += size;
}
}
}
}