Flex 利用 sprit 实现字体 竖排 旋转

    字体旋转竖排,最近一直在网上找,大多是利用TLF等,太麻烦.终于被我找到了集成Sprit来解决这个问题,封装了一下,贴出来,备用:

集成Sprit的核心类

package  
{  
	import flash.display.Sprite;
	import flash.text.engine.ElementFormat;
	import flash.text.engine.FontDescription;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextElement;
	import flash.text.engine.TextLine;
	import flash.text.engine.TextRotation;  
	
	public class RoutaTextAs extends Sprite{//"MS Mincho"  一个字体
		/**
		 * 旋转字体
		 * @param text 待旋转的文档
		 * @param fontSize 
		 * @param fontType 
		 * @param rotate 旋转度
		 * @param theHeight 高
		 * @param theWidth  宽
		 * @param x
		 * @param y
		 * @param spaceBetween 字体空间
		 * 
		 */
		public function RoutaTextAs(text:String="" , fontSize:int=15 ,
									 fontType:String="宋体" , rotate:String= TextRotation.ROTATE_90 
									 , theHeight:int=60 , theWidth:int=60, x:int=0 , y:int=0
									, spaceBetween:int=20):void {  
			var format:ElementFormat = new ElementFormat();  
			format.fontSize = fontSize;  
			format.fontDescription = new FontDescription(fontType);  
			
			var textBlock:TextBlock = new TextBlock();  
			textBlock.content =  new TextElement(text, format);  
			textBlock.lineRotation = rotate;//方向  
			var linePosition:Number =x;  
			var previousLine:TextLine = null;  
			
			while (true) {  
				var textLine:TextLine = textBlock.createTextLine(previousLine, theHeight);  
				trace(this.width);
				if (textLine == null || this.width > theWidth)  { break;  }
				textLine.y = y;      
				textLine.x = linePosition;  
				linePosition -= spaceBetween;  
				addChild(textLine);                  
				previousLine = textLine;  
			}          
		}  
	}  
}  

  Spirt必须add在UIComponent里面,包装了一层,可以直接使用:

<?xml version="1.0" encoding="utf-8"?>
<mx:UIComponent xmlns:mx="http://www.adobe.com/2006/mxml" initialize="init()">
	<mx:Script>
		<![CDATA[
			import flash.text.engine.TextRotation;
			
			import mx.events.FlexEvent;
			
			public var text:String = "";//要加入的字体
			public var fontSize:int = 15 ;
			public var fontType:String ="宋体";
			public var rotate:String = TextRotation.ROTATE_90;//旋转角度
			public var spaceBetween:int = 18;//旋转角度
			
			protected function init():void{
				var tt:RoutaTextAs =  new RoutaTextAs(text
					, fontSize , fontType , rotate , this.height , this.width
					,this.x , this.y , spaceBetween);
				this.addChild(tt);
			}
			
		]]>
	</mx:Script>
	
</mx:UIComponent>

 

 

一些参数需要自己调整,下面是Application

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" 
				layout="absolute" initialize="application1_initializeHandler(event)">  
	<mx:Script>
		<![CDATA[
			import flash.text.engine.TextRotation;
			
			import mx.core.UIComponent;
			import mx.events.FlexEvent;
			protected function application1_initializeHandler(event:FlexEvent):void{
				var rr:RoutaText = new RoutaText();
				rr.height = 60;
				rr.width = 150;
				rr.spaceBetween = 55;
				rr.text = "猩红的撒旦斯蒂芬桑德菲杰撒" +
					"地方速度快李凤江桑德菲杰思考了打飞机克里" +
					"斯丁积分卡螺丝刀积分卡螺丝" +
					"刀杰弗里斯大开间付款了三等奖分散度看了 飞";
				ttt.addChild(rr);
				
			}
			
		]]>
	</mx:Script>
	<mx:Box id="ttt" x="409" y="151" width="250"/>
	
</mx:Application>  

 

用吧!

 

 

RoutaTextAs  修改了一下.支持横排很竖排
package 
{  
	import flash.display.Sprite;
	import flash.text.engine.ElementFormat;
	import flash.text.engine.FontDescription;
	import flash.text.engine.TextBlock;
	import flash.text.engine.TextElement;
	import flash.text.engine.TextLine;
	import flash.text.engine.TextRotation;  
	
	public class RoutaTextAs extends Sprite{//"MS Mincho"  一个字体
		/**
		 * 旋转字体
		 * @param text 待旋转的文档
		 * @param fontSize 
		 * @param fontType 
		 * @param rotate 旋转度
		 * @param theHeight 高
		 * @param theWidth  宽
		 * @param x
		 * @param y
		 * @param spaceBetween 字体空间
		 * 
		 */
		public function RoutaTextAs(text:String="" , fontSize:int=15 ,
									fontType:String="宋体" , rotate:String= TextRotation.ROTATE_90 
									, theHeight:int=60 , theWidth:int=60, x:int=0 , y:int=0
									  , spaceBetween:int=20):void {  
			var format:ElementFormat = new ElementFormat();  
			format.fontSize = fontSize;  
			format.fontDescription = new FontDescription(fontType);  
			var textBlock:TextBlock = new TextBlock();  
			textBlock.content =  new TextElement(text, format);  
			textBlock.lineRotation = rotate;//方向  
			
			var previousLine:TextLine = null;  
			if(rotate == TextRotation.ROTATE_90){
				var linePosition:Number = x;  
				while (true) {  
					var textLine:TextLine = textBlock.createTextLine(previousLine, theHeight);  
					if (textLine == null || this.width > theWidth)  { break;  }
					textLine.y = y;      
					textLine.x = linePosition;  
					linePosition += spaceBetween;  
					addChild(textLine);                  
					previousLine = textLine;  
				}          
			}else if(rotate == TextRotation.ROTATE_0){
				var linePosition:Number = y + fontSize ;  
				while (true) {  
					var textLine:TextLine = textBlock.createTextLine(previousLine, theWidth);  
					if (textLine == null ||( this.height+fontSize) >= theHeight)  { break;  }
					textLine.y = linePosition;      
					textLine.x = x;  
					linePosition += spaceBetween;  
					addChild(textLine);                  
					previousLine = textLine;  
				}          
			}
		}  
	} 
}  

你可能感兴趣的:(Flex)