Skip to main content
May 26, 2009
Question

simple textflow maxchar ??

  • May 26, 2009
  • 1 reply
  • 1024 views

Hi guys,

I thought I would experiment with textLayout. I don't think I want to delve as deep as flash.text.engine.

Before I get onto more complicated component design I thought it would be simple enough take a TextFlow Instance and build restrictiveness into it until it performed exactly like a textfield. That is, a text flow with a limit of one line, "enter" key loss of focus, and a maxchar parameter and a password parameter.

I though maxchar would be easy ... apparently not - or I am misunderstanding a fundamental aspect of textLayout or I'm constantly overlooking something stupid in my code.

When I compile the code against the latest 437 build (as adopted from the nov08 API which also didn't work) I get :

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at flashx.textLayout.edit::ElementRange$/createElementRange()

at flashx.textLayout.operations::InsertTextOperation/doOperation()

at flashx.textLayout.edit::EditManager/doInternal()

at flashx.textLayout.edit::EditManager/doOperation()

at flashx.textLayout.edit::EditManager/flushPendingOperations()

at flashx.textLayout.edit::SelectionManager/enterFrameHandler()

package {

     import flash.display.MovieClip;

     import flashx.textLayout.container.ContainerController;

     import flashx.textLayout.elements.TextFlow;

     import flashx.textLayout.elements.ParagraphElement;

     import flashx.textLayout.elements.SpanElement;

     import flashx.textLayout.edit.EditManager;

     import flashx.textLayout.formats.TextLayoutFormat;

     //import flashx.textLayout.operations.FlowOperation;

     import flashx.textLayout.events.CompositionCompletionEvent;

     public class testVellum extends MovieClip {

          private var tfw : Number;

          private var tfh : Number;

          private var inText : String;

          //private var password : Boolean;

          //stylers

          private var fontSize : int;

          private var color : uint;

          private var kern : Number;

          private var kernLeft : Number;

          private var kernRight : Number;

          private var fontF : String;

          private var cFormat : TextLayoutFormat;

          private var tf : TextFlow;

          private var pe : ParagraphElement;

          private var se : SpanElement;

          private var tfem : EditManager;

          private var seForced : String;

          private var maxLength : int;

          public function testVellum():void{

               //set the defaults

               color = 0x272727;

               fontSize = 18;

               inText = "testerText";

               kern = 0;

               kernLeft = 0;

               kernRight = 0;

               fontF = "Arial, Helvetica, _sans";

               gonow();

          }

          public function gonow():void{

               cFormat = new TextLayoutFormat();

               tf = new TextFlow();

               pe = new ParagraphElement();

               se = new SpanElement();

               tfem = new EditManager();

               tf.interactionManager = tfem;

               cFormat.fontSize = fontSize;

               cFormat.fontFamily = fontF;

               se.text = inText;

               se.format = cFormat;

               pe.addChild(se);

               tf.addChild(pe);

               tf.flowComposer.addController(new ContainerController(this, tfw, tfh));

               tf.flowComposer.updateAllControllers();

               tf.addEventListener(CompositionCompletionEvent.COMPOSITION_COMPLETE, compHandler);

          }

          private function compHandler(e : CompositionCompletionEvent):void{

               if(se.textLength > maxLength){

                    seForced = se.text.substr(0, maxLength);

                    se.replaceText(0, se.text.length, seForced);

               }

          }

     }

}

This topic has been closed for replies.

1 reply

May 26, 2009

resolved with help from robin.briggs over on this thread : http://forums.adobe.com/thread/422806?tstart=30

- Listening for the wrong event.. using a span element instead of the entire textflow.

fixed code should it be of care to someone else:

package {

import flash.display.MovieClip;

import flashx.textLayout.container.ContainerController;

import flashx.textLayout.elements.TextFlow;

import flashx.textLayout.elements.ParagraphElement;

import flashx.textLayout.elements.SpanElement;

import flashx.textLayout.edit.EditManager;

import flashx.textLayout.edit.IEditManager;

import flashx.textLayout.formats.TextLayoutFormat;

import flashx.textLayout.events.CompositionCompletionEvent;

import flashx.textLayout.events.FlowOperationEvent;

public class testVellum extends MovieClip {

private var tfw : Number;

private var tfh : Number;

private var inText : String;

//private var password : Boolean;

//stylers

private var fontSize : int;

private var color : uint;

private var kern : Number;

private var kernLeft : Number;

private var kernRight : Number;

private var fontF : String;

private var cFormat : TextLayoutFormat;

private var tf : TextFlow;

private var pe : ParagraphElement;

private var se : SpanElement;

private var tfem : EditManager;

private var seForced : String;

private var maxLength : int;

public function testVellum():void{

//set the defaults

color = 0x272727;

fontSize = 18;

inText = "testerText";

kern = 0;

kernLeft = 0;

kernRight = 0;

fontF = "Arial, Helvetica, _sans";

tfw = 200, tfh = 20;

maxLength = 9;

gonow();

}

public function gonow():void{

cFormat = new TextLayoutFormat();

tf = new TextFlow();

pe = new ParagraphElement();

se = new SpanElement();

tfem = new EditManager();

tf.interactionManager = tfem;

cFormat.fontSize = fontSize;

cFormat.fontFamily = fontF;

se.text = inText;

se.format = cFormat;

pe.addChild(se);

tf.addChild(pe);

tf.flowComposer.addController(new ContainerController(this, tfw, tfh));

            tf.flowComposer.updateAllControllers();

tf.interactionManager = tfem;

tf.addEventListener(FlowOperationEvent.FLOW_OPERATION_END, compHandler, false, 0, true);

}

private function compHandler(e : FlowOperationEvent):void{

if ((tf.textLength - 1)> maxLength){

tf.interactionManager.setSelection(maxLength,tf.textLength - 1);

(tf.interactionManager as IEditManager).deleteText();

}

}

}

}