/** * Bitmap Text - turn text fields into bitmaps * by Corey von Birnbaum (coldconstructs.com) * Distributed under the MIT license * http://www.opensource.org/licenses/mit-license.php */ package com.coldconstructs.graphics { import flash.display.*; import flash.text.*; /** * USAGE * // Optional format * var format:TextFormat = new TextFormat(); * format.font = "Verdana"; * format.size = 10; * * var bmt:BitmapText = new BitmapText(300, 500, true, format, 12, 100); * bmt.addText("
This is html text, which can't be mixed with regular text.
", true); * bmt.addText("This is also html text, with different font height.
", true);
* bmt.addText("So I've set the average font height to 12 to compensate for the size discrapency.
And added 100 pixels of padding just in case.
", true); * this.addChild(bmt.render()); */ public class BitmapText { private var tf:TextField = new TextField(); private var avgPointSize:Number; private var padding:int; public function BitmapText(nWidth:int, nHeight:int, multiline:Boolean = true, fontFormat:TextFormat = null, avgPointSize:Number = 12, pad:int = 0) { tf.width = nWidth; tf.height = nHeight; tf.multiline = multiline; tf.wordWrap = multiline; // No point in wrapping if it's not multiline padding = pad; /* avgPointSize helps determine how long the resulting bitmap should be based on how much text there is and how big it is. So for example if you add htmlText that's size 10 but has titles that are 14pt (and/or some paragraph splits), you would pass 13 so that the height of the resulting bitmap ecompasses ALL your text */ this.avgPointSize = avgPointSize; if (fontFormat != null) tf.defaultTextFormat = fontFormat; } public function addText(string:String, htmlType:Boolean = false):void { htmlType ? tf.htmlText += string : tf.appendText(string); } public function render():Bitmap { var bmd:BitmapData = new BitmapData(tf.width, (avgPointSize * tf.numLines + padding), true, 0x00000000); bmd.draw(tf); tf = null; var bmp:Bitmap = new Bitmap(bmd); return bmp; } } // end class }