使用自定义字体时,如何在TLFTextField中居中文本?

时间:2012-04-27 18:20:01

标签: actionscript-3 flash-cs5

按照标题,我该怎么办?这是一个反映我的场景的最小工作示例:

import fl.text.TLFTextField;
import flashx.textLayout.formats.*;
import flashx.textLayout.elements.TextFlow;
import flashx.textLayout.edit.*;

var tf:TLFTextField = new TLFTextField();
tf.embedFonts = true;
tf.text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean suscipit semper viverra.';
var format:TextLayoutFormat = new TextLayoutFormat();
format.fontFamily = 'MyPlainFont';
format.fontLookup = FontLookup.EMBEDDED_CFF;    
format.fontSize = 14;
format.textAlign = TextAlign.CENTER;

var editManager:EditManager = new EditManager();
var sel:SelectionState = new SelectionState(tf.textFlow, 0, 86);
tf.textFlow.interactionManager = editManager;
editManager.applyLeafFormat(format, sel);

var format2:TextLayoutFormat = new TextLayoutFormat();
format2.fontFamily = 'MyBoldFont';
format2.fontLookup = FontLookup.EMBEDDED_CFF;
format2.fontSize = 14;
format2.textAlign = TextAlign.CENTER;
var sel2:SelectionState = new SelectionState(tf.textFlow, 0, 20);
editManager.applyLeafFormat(format2, sel2);

tf.width = 100;
tf.wordWrap = true;

addChild(tf);

3 个答案:

答案 0 :(得分:0)

对于普通文本字段,您必须在添加文本之前指定格式,否则它不适用,尝试过吗?

答案 1 :(得分:0)

以下代码可以解决您的问题。就像Sidrich说的那样,总是设置文本属性!我还添加了autoSize属性,因为如果没有设置这个,我看到了一些奇怪的定位故障。

import fl.text.TLFTextField;
import flashx.textLayout.formats.TextLayoutFormat;
import flashx.textLayout.edit.EditManager;
import flashx.textLayout.formats.TextAlign;
import flashx.textLayout.edit.SelectionState;
import flash.text.TextFieldAutoSize;

var tf:TLFTextField = new TLFTextField();
tf.embedFonts = true;
tf.border = true;

var format:TextLayoutFormat = new TextLayoutFormat();
format.fontFamily = 'MyPlainFont';
format.fontLookup = FontLookup.EMBEDDED_CFF;    
format.fontSize = 14;
format.textAlign = TextAlign.CENTER;
var editManager:EditManager = new EditManager();
var sel:SelectionState = new SelectionState(tf.textFlow, 0, 86);
tf.textFlow.interactionManager = editManager;
editManager.applyLeafFormat(format, sel);

var format2:TextLayoutFormat = new TextLayoutFormat();
format2.fontFamily = 'MyBoldFont';
format2.fontLookup = FontLookup.EMBEDDED_CFF;
format2.fontSize = 14;
format2.textAlign = TextAlign.CENTER;
var sel2:SelectionState = new SelectionState(tf.textFlow, 0, 20);
editManager.applyLeafFormat(format2, sel2);

tf.width = 100;
tf.wordWrap = true;
tf.autoSize = TextFieldAutoSize.CENTER;
tf.text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean suscipit semper viverra.';

addChild(tf);

答案 2 :(得分:0)