Text Layout Engine: Fit text into rectangle
Utorok, Október 13th, 2009Table of contents for Text Layout Engine
- Text Layout Engine in Flex 3
- Text Layout Engine: Fit text into rectangle
- Text Layout Engine: TruncationOptions
This is first example in serie of examples of showcase how to use Text Layout Engine (mainly for Flex 3).
Here is quick description: I need to fit text in rectangle (need it for our Goalscape project). I want to be able to change width, height, font size and text of course. In Text Layout Engine this can be done very easily. All you need to do is create correct format for your text, and set compositionBounds for text line factory. I will use StringTextLineFactory class, which provides a simple way to create TextLines from a string. Even I add also TruncationOptions, which specifies options for limiting the number of lines of text created by a text line factory and for indicating when lines have been left out. I’m using TruncationOptions, because I want use maximum of 3 lines and if text is too long I want add … at the end. This is done automatically in Text Layout Engine and I do not need to implement by myself.
Only problem I see, that result .swf file size is over 500kB even builded as Release version (Debug version has 692kB). It’s because of Text Layout Engine and Flex framework are included in .swf. But because I will use it in AIR application, it’s ok for me.
I have created Flash demo for you, but it works just in Flash Player 10, because of Text Layout Engine. Just play with sliders and write your custom text to see effect. In case text will be truncating you will see green square, otherwise there will be red square (not truncated).
id="TextLayoutEngineExamples" width="100%" height="350"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">
width="100%" height="350" name="TextLayoutEngineExamples" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer">
And this is source code
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" applicationComplete="fit()"> <mx:Style source="styles/fonts.css"/> <mx:VBox width="100%" height="100%"> <mx:HBox width="100%"> <mx:Label text="Text:"/> <mx:TextInput id="txtInput" width="100%" change="fit()" text="Text Layout Engine: Fit Text into Rectangle"/> </mx:HBox> <mx:HBox> <mx:Label text="Font Size:"/> <mx:HSlider id="fsSlider" liveDragging="true" minimum="6" maximum="50" snapInterval="1" value="14" change="fit()"/> </mx:HBox> <mx:HBox> <mx:Label text="Text Width:"/> <mx:HSlider id="wSlider" liveDragging="true" minimum="0" maximum="500" snapInterval="1" value="100" change="fit()"/> </mx:HBox> <mx:HBox> <mx:Label text="Text Height:"/> <mx:HSlider id="hSlider" liveDragging="true" minimum="0" maximum="500" snapInterval="1" value="100" change="fit()"/> </mx:HBox> <mx:HBox> <mx:Label text="isTruncated?"/> <mx:Canvas width="20" height="20" backgroundColor="0xff0000" visible="{!isTruncated}" includeInLayout="{!isTruncated}"/> <mx:Canvas width="20" height="20" backgroundColor="0x00aa00" visible="{isTruncated}" includeInLayout="{isTruncated}"/> </mx:HBox> <mx:UIComponent id="ui" width="100%" height="100%"> </mx:UIComponent> </mx:VBox> <mx:Script> <![CDATA[ import flashx.textLayout.factory.TruncationOptions; import flash.text.engine.Kerning; import flash.text.engine.FontLookup; import flash.text.engine.RenderingMode; import flashx.textLayout.formats.TextLayoutFormat; import flashx.textLayout.factory.StringTextLineFactory; private var sprite:Sprite; [Bindable] public var isTruncated:Boolean; public function fit():void { if (sprite == null) { sprite = new Sprite(); ui.addChild(sprite); } while (sprite.numChildren > 0) { sprite.removeChildAt(0); } var factory:StringTextLineFactory = new StringTextLineFactory(); factory.compositionBounds = new Rectangle( 10, 10, wSlider.value, hSlider.value ); var format:TextLayoutFormat = new TextLayoutFormat(); format.fontFamily = "Verdana, _sans"; format.fontSize = fsSlider.value; format.color = 0x000000; format.textAlpha = .9; format.kerning = Kerning.ON; format.renderingMode = RenderingMode.CFF; format.fontLookup = FontLookup.EMBEDDED_CFF; factory.spanFormat = format; var truncationOptions:TruncationOptions = new TruncationOptions('...',3); factory.truncationOptions = truncationOptions; factory.text = txtInput.text; factory.createTextLines( useTextLines ); isTruncated = factory.isTruncated; sprite.graphics.clear(); sprite.graphics.beginFill(0x555555,.5); sprite.graphics.drawRect( 10,10, wSlider.value, hSlider.value ); sprite.graphics.endFill(); } private function useTextLines( line:DisplayObject ):void { var displayObject:DisplayObject = sprite.addChild( line ); } ]]> </mx:Script> </mx:Application>
No Comments Yet
You can be the first to comment!
Sorry, comments for this entry are closed at this time.