flex 文字竖排_Flex 利用 sprit 实现字体 竖排 旋转

该博客介绍了如何利用Flex和Sprit技术实现文字的竖排和横排旋转。通过自定义RoutaTextAs类,设置旋转角度、字体大小、字体类型等参数,可以方便地在UI组件中实现文字排列效果。示例代码展示了如何创建和使用这个类,以展示文字旋转和布局的灵活性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

字体旋转竖排,最近一直在网上找,大多是利用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里面,包装了一层,可以直接使用:

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);

}

]]>

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

layout="absolute" initialize="application1_initializeHandler(event)">

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);

}

]]>

用吧!

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;

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值