2013-05-28 19 views

回答

0

這可以通過各種方式完成。

你可以稱之爲一個fitLabel()傳遞文本字段和邊框:

fitLabel(textField, new Rectangle(0, 0, stage.stageWidth, stage.stageHeight)); 

一種方法將縮放文本字段的邊界:

protected function fitLabel(textField:TextField, bounds:Rectangle):void 
{ 
    var metrics:TextLineMetrics = textField.getLineMetrics(0); 
    var ratio:Number = Math.min(bounds.width/metrics.width, 
           bounds.height/metrics.height); 

    textField.scaleX = ratio; 
    textField.scaleY = ratio; 
} 

另一種方法是實現一個算法,調整字體大小佔DPI:

protected var dpi:uint = 72; 

protected function fitLabel(textField:TextField, bounds:Rectangle):void 
{ 
    var metrics:TextLineMetrics = textField.getLineMetrics(0); 

    var format:TextFormat = textField.getTextFormat(); 
    format.size = (stage.stageWidth/dpi) * 13; 

    textField.setTextFormat(format); 
    textField.width = bounds.width; 
    textField.height = bounds.height; 
} 

另一種方法是循環遍歷每次增加字體大小的循環,直到最大寬度達到邊界。

Example SWF

hello-world

+0

了詳細的解釋十分感謝。 – puks1978

相關問題