In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

TextField magic - floating test

Get Adobe Flash player
by leichtgewicht 07 May 2012
// forked from makc3d's TextField magic
package {
    import flash.text.TextFormatAlign;
    import flash.display.StageScaleMode;
    import flash.display.StageAlign;
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.display.Stage;
    
    public class FlashTest extends Sprite {
        
        private var tf: TextField = new TextFieldImageHelper();
        
        public function FlashTest() {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            tf.multiline = true;
            tf.wordWrap = true;
            tf.htmlText = "First Line <img width='20' height='30'/><br/>"+imgHtml("left")+"Text on the right<br>Text on the right<br>Text on the right<br>20 px later<br>"+imgHtml("right", TextFormatAlign.RIGHT)+"Text on the left<br/>Text on the left<br/>Text on the left<br/>";
            tf.width = stage.stageWidth;
            tf.height = stage.stageHeight;
            addChild(tf);
            
            drawContent();
        }
        
        private function imgHtml(id: String, align: String = "left", width: int=20, height: int=20): String {
            return "<img src='flash.display.Sprite' id='"+id+"' width='"+width+"' height='"+height+"' align='"+align+"'/>";
        }

        
        private function drawContent(): void {
            graphics.clear();
            
            draw("left");
            draw("right");
        }
        
        private function draw(imageId:String): void {
            var image: Sprite = tf.getImageReference(imageId) as Sprite;
            drawRect(image.x, image.y);
        }
    
        private function drawRect(x: Number, y: Number): void {
            graphics.beginFill(0xFF0000);
            graphics.drawRect(x, y, 20, 20);
        }
    }
}
import flash.text.TextField;

class TextFieldImageHelper extends TextField {
    
    
    override public function set htmlText(htmlText: String): void {
        log(
            subRegExp(
                htmlText,
                /<img(\s*\w*\s*=('|")[a-z0-9.]*('|"))*\s*\/>/gi,
                /<img((?P<width>(\s*width\s*=('|")\w*('|")))|(?P<height>(\s*height\s*=('|")\w*('|")))|(\s*\w*\s*=('|")[a-z0-9.]*('|")))*\s*\/>/i
            )
        );
        super.htmlText = htmlText;
    }

    
    private function createImage(id: String): void {

    }

}

function subRegExp(str: String, regExp: RegExp, subRegExp: RegExp): Array {
    var result: Array = [];
    var data: Object;
    while( (data = regExp.exec(str)) != null ) {
        var subData: Object
        var subResult: Object = {};
        var miniData: String = data[0];
        log("==> "+ miniData);
        while( subData = subRegExp.exec(miniData) ) {
            for( var key: String in subData ) {
                if( isNaN(parseInt(key)) && key != "index" && key != "input" ) {
                    log(key+" => "+subData[key]);
                    if( !subResult[key] ) {
                        subResult[key] = subData[key];
                    }
                }
            }
        }
        result.push(subResult);
    }
    return result;
}


class ImageData {
    public var id: String;
    public var width: int;
    public var height: int;
    public var x: int;
    public var y: int;
}



var log: Function = Wonderfl.log;