Posts Tagged ‘Text Layout Framework’

[AS3] How to create one line editable TextFlow of Text Layout Framework

Monday, July 25th, 2011

I want to create one line editable TextFlow, not multiple line, with Text Layout Framework(TLF).

First, you need to create a subclass of EditManager.
This class can change the behavior when keyboard is pressed.

package
{
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	
	import flashx.textLayout.edit.EditManager;
	import flashx.undo.IUndoManager;
	
	public class CustomEditManagner extends EditManager
	{
		public function CustomEditManagner(undoManager:IUndoManager=null)
		{
			super(undoManager);
		}
		
		override public function keyDownHandler(event:KeyboardEvent):void
		{
			if(event.keyCode != Keyboard.ENTER){
				super.keyDownHandler(event);
			}		
		}
	}
}

Second, you create the TextFlow, and assign above custom class to its interactionManager.

package
{
	import flash.display.Sprite;
	import flashx.textLayout.formats.LineBreak;
	
	import flashx.textLayout.container.ContainerController;
	import flashx.textLayout.conversion.TextConverter;
	import flashx.textLayout.edit.EditManager;
	import flashx.textLayout.elements.TextFlow;
	
	[SWF(width="450", height="450", frameRate="30")]
	public class OneLineEditableTLF extends Sprite
	{
		public function OneLineEditableTLF()
		{
			createTextLine();
		}
		
		private function createTextLine():void
		{
			var sp:Sprite = new Sprite();
			addChild(sp);
			sp.x = 100;
			sp.y = 50;
			
			var markup:XML = 

Hello, World

; var textFlow:TextFlow = TextConverter.importToFlow(markup, TextConverter.TEXT_LAYOUT_FORMAT); var containerController:ContainerController = new ContainerController(sp, 200, 16); textFlow.flowComposer.addController(containerController); var editManager:EditManager = new CustomEditManagner(); textFlow.interactionManager = editManager; textFlow.fontSize = 16; textFlow.lineBreak = LineBreak.EXPLICIT; textFlow.flowComposer.updateAllControllers(); } } }