import flash.text.engine.*;
import flash.display.Sprite;
var str: String = "一般情况下,在 Flash 中我们使用最常见 TextField 对象(最传统的文本引擎)进行文字排版处理,TextField 允许开发者通过 CSS 样式设置文本的版式。\r在 CSS 样式说明中,可以查到通过 text-align 属性值设置为 justify (text-align: justify;)即可实现左右对齐,但实际上开发者们往往会发现这个属性值在应用到中文文字的排版中时,它往往是没有任何效果的(包括很多网页开发者其实也是一样的)。\r这是因为 text-align: justify;这个设置是通过对重置空格的大小实现的,所以这个属性和值只对英文文字的版式有效果,因为英文单词是通过空格来间隔的,而纯中文的中文字符排版时它们之间往往是没有任何空格字符的,所以它往往是没有效果的,除非在中文字符或标点字符里面混入一些空格,以便让 text-align: justify; 这个属性值发挥作用。\r\r从 FlashPlayer 10 开始提供了全新的文本引擎(FTE,其中 TLF 就是基于 FTE 的包装类),并且还提供了 JustificationStyle 类用于控制避头尾法则(所谓的避头尾法则,其实就是指一些特殊的字符是不能出现在行首或行尾的,最常见的像逗号,句号等不能出现行首的)。";
var format: ElementFormat = new ElementFormat(null, 14, 0x006699);
//format.fontSize = 14;
//format.color = 0x006699;
//format.alpha = 0.9;
var textElement: TextElement = new TextElement(str, format);
var spaceJustifier: SpaceJustifier = new SpaceJustifier("en", LineJustification.ALL_BUT_MANDATORY_BREAK);
var textBlock: TextBlock = new TextBlock();
textBlock.content = textElement;
textBlock.textJustifier = spaceJustifier; //你可以通过注销这一行来对比前后效果
createLines(textBlock);
function createLines(textBlock: TextBlock): void {
var yPos = 20;
var textLineWidth: int = 400;
var textLine: TextLine = textBlock.createTextLine(null, textLineWidth);
while (textLine) {
addChild(textLine);
textLine.x = 15;
yPos += textLine.textHeight + 8;
textLine.y = yPos;
textLine = textBlock.createTextLine(textLine, textLineWidth);
}
}
/*
LineJustification.
------
ALL_BUT_LAST : String = "allButLast"
[静态] 两端对齐最后一行除外的所有行。
ALL_BUT_MANDATORY_BREAK : String = "allButMandatoryBreak"
[静态] 两端对齐最后一行和强制结束的行除外的所有行。
ALL_INCLUDING_LAST : String = "allIncludingLast"
[静态] 两端对齐所有行。
UNJUSTIFIED : String = "unjustified"
[静态] 不要两端对齐行。
*/
文本的两端对齐方法终于在http://blog.zinewow.com/post/333.html这里找到方法了。
感觉EastAsianJustifier比SpaceJustifier的缩进方式好一些,特别是开头空两个全角空格的情况下,两者区别没具体研究。
不过最后一行不想两端对齐需要改成LineJustification.ALL_BUT_MANDATORY_BREAK
此方法不是传统的动态文本,我发现每一行文字都是while输出的,难怪新版的flash CC在UI界面上删除了TLF文本输入,这种文本初始效率肯定不好,Adobe工程师也不愿维护了(话说9102年已死的flash也不可能有维护了),有需求时在说,先记下
API:https://help.adobe.com/zh_CN/as3/dev/WS9dd7ed846a005b294b857bfa122bd808ea6-8000.html
下面是横版与竖版文字的切换:
package {
import flash.text.engine.TextBlock;
import flash.text.engine.TextLine;
import flash.text.engine.TextElement;
import flash.text.engine.ElementFormat;
import flash.text.engine.TextRotation;
import flash.text.engine.TextBaseline;
import flash.text.engine.LineJustification;
import flash.text.engine.FontDescription;
import flash.text.engine.EastAsianJustifier;
import flash.display.Loader;
import flash.display.Sprite;
import flash.display.Stage;
import flash.events.MouseEvent;
import flash.system.Capabilities;
public class TextBlockExample extends Sprite {
var vertical:Boolean;
var container:Sprite;
var textBlocks:Vector.<TextBlock>;
var loader:Loader = new Loader();
public function TextBlockExample():void {
stage.addEventListener(MouseEvent.CLICK, clickHandler);
createContent();
createLines();
}
private function createEmptyBlock():TextBlock {
var textBlock:TextBlock = new TextBlock();
textBlock.baselineZero = TextBaseline.IDEOGRAPHIC_CENTER;
textBlock.textJustifier = new EastAsianJustifier("ja", LineJustification.ALL_BUT_MANDATORY_BREAK);
textBlock.lineRotation = vertical? TextRotation.ROTATE_90: TextRotation.ROTATE_0;
return textBlock;
}
private function paragraph1(format:ElementFormat):TextBlock {
var textBlock:TextBlock = createEmptyBlock();
textBlock.content = new TextElement("一般情况下,在 Flash 中我们使用最常见 TextField 对象(最传统的文本引擎)进行文字排版处理,TextField 允许开发者通过 CSS 样式设置文本的版式。\r在 CSS 样式说明中,可以查到通过 text-align 属性值设置为 justify (text-align: justify;)即可实现左右对齐,但实际上开发者们往往会发现这个属性值在应用到中文文字的排版中时,它往往是没有任何效果的(包括很多网页开发者其实也是一样的)。\r这是因为 text-align: justify;这个设置是通过对重置空格的大小实现的,所以这个属性和值只对英文文字的版式有效果,因为英文单词是通过空格来间隔的,而纯中文的中文字符排版时它们之间往往是没有任何空格字符的,所以它往往是没有效果的,除非在中文字符或标点字符里面混入一些空格,以便让 text-align: justify; 这个属性值发挥作用。\r\r从 FlashPlayer 10 开始提供了全新的文本引擎(FTE,其中 TLF 就是基于 FTE 的包装类),并且还提供了 JustificationStyle 类用于控制避头尾法则(所谓的避头尾法则,其实就是指一些特殊的字符是不能出现在行首或行尾的,最常见的像逗号,句号等不能出现行首的)。",
format);
return textBlock;
}
private function paragraph2(format:ElementFormat):TextBlock {
var textBlock:TextBlock = createEmptyBlock();
textBlock.content = new TextElement("My web: www.flashme.cn | 夕空", format);
return textBlock;
}
private function paragraph3(format:ElementFormat):TextBlock {
var textBlock:TextBlock = createEmptyBlock();
textBlock.content = new TextElement(
String.fromCharCode(0x3010) +
"2019" +
String.fromCharCode(0x5E74) + "9" + String.fromCharCode(0x6708) +
"2" +
String.fromCharCode(0x65E5, 0x3011),
format);
return textBlock;
}
private function createContent():void {
var font:FontDescription = new FontDescription();
if (Capabilities.os.search("Mac OS") > -1)
font.fontName = String.fromCharCode(0x5C0F, 0x585A, 0x660E, 0x671D) + " Pro R";
else
font.fontName = "Kozuka Mincho Pro R";
var format:ElementFormat = new ElementFormat();
format.fontDescription = font;
format.fontSize = 14;
format.locale = "ja";
format.color = 0x000000;
if (!vertical)
format.textRotation = TextRotation.ROTATE_0;
textBlocks = new Vector.<TextBlock>();
textBlocks.push(
paragraph1(format),
paragraph2(format),
paragraph3(format)
);
}
private function createLines():void {
if (container != null) {
removeChild(container);
}
container = new Sprite();
container.y = 45;
container.x = 40;
addChild(container);
var linePosition:Number = vertical? this.stage.stageWidth - 120: 0;
for (var i:uint = 0; i < textBlocks.length; i++) {
var textBlock:TextBlock = textBlocks[i];
var previousLine:TextLine = null;
while (true) {
var textLine:TextLine = textBlock.createTextLine(
previousLine,
500);
if (textLine == null)
break;
if (vertical)
{
textLine.x = linePosition;
linePosition -= 24;
}
else
{
textLine.y = linePosition;
linePosition += 24;
}
container.addChild(textLine);
previousLine = textLine;
}
if (vertical)
linePosition -= 16;
else
linePosition += 16;
}
}
private function clickHandler(event:MouseEvent):void {
vertical = !vertical;
createContent();
createLines();
}
}
}