2012-04-27 94 views
0

按照標題,我該怎麼辦?下面是一個最小的工作例子反映了我的情況:如何在使用自定義字體時將TLFTextField中的文本居中?

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

我不會使用TLFTextField,它有問題。 – atilkan 2012-05-06 23:02:06

回答

0

對你有添加文本,否則它不適用前ASIGN格式正常的TextField,已嘗試?

+0

是的,它以它爲中心,但它不會正確應用字體,它將只使用最後應用的字體。 – RedDragon 2012-05-10 13:30:19

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

通過最後添加文本將居中,但不會將自定義字體應用於文本,它將僅使用最後應用的字體。 – RedDragon 2012-05-10 13:31:18

+0

關於嵌入字體,您可以閱讀[this](http://marumushi.com/news/embedding-fonts-in-as3)鏈接。 – CGBe 2012-05-10 14:04:18

相關問題