2011-04-19 32 views
2

我想在ActionScript中將一些文本嵌入到圓中。我有三個問題:我無法將文本置於圓圈中央,無法使文本居中對齊,也無法將字體應用於文本。關於字體,我知道它嵌入正確,因爲它在舞臺上創作的TextField上。在形狀中嵌入文本

[Embed(source="DAXCOMPL.TTF", fontName="DaxCompact-Light", mimeType='application/x-font', embedAsCFF='false')] 
private var MyFont:Class; 

public function Bubble(...) { 
    var myFont:Font = new MyFont(); 

    var myFormat:TextFormat = new TextFormat(); 
    myFormat.size = 20; 
    myFormat.align = TextFormatAlign.CENTER; 
    myFormat.font = myFont.fontName; 

    var circle:Sprite = new Sprite(); 
    var r:int = 30; 
    var text:TextField = new TextField(); 
    text.text = "Hello world!"; 
    text.wordWrap = true; 
    text.defaultTextFormat = myFormat; 
    text.autoSize = TextFieldAutoSize.LEFT; 
    text.x = -30; 
    text.y = -30; 

    circle.graphics.lineStyle(2, 0x000000, 1.0); 
    circle.graphics.drawCircle(0,0,r); 
    circle.graphics.endFill(); 
    circle.addChild(text); 
    circle.x = 75; 
    circle.y = 450; 
    addChild(circle); 
} 

回答

1

嘗試initalize文本字段是這樣的:

var text:TextField = new TextField(); 
text.embedFonts = true; // use embedded font 
text.defaultTextFormat = myFormat; // use this command before setting text 
text.text = "Hello world!"; 
text.wordWrap = true; 
text.autoSize = TextFieldAutoSize.LEFT; 
text.x = -text.textHeight*0.5; //center the textfield after setting text 
text.y = -text.textWidth*0.5; 
+0

對不起,我在回答延遲,但是這解決了兩個我的三個問題(垂直對齊我的文字和運用我的字體)。但是,文字仍然沒有與形狀的中心對齊 - 它總是結果太靠右。這對你有用嗎? – 2011-05-04 16:55:16