2012-12-03 46 views
1

我目前正在尝试基于全局样式表通过基本字体大小变量动态设置文本大小。我目前正在使用方法tfWidthController来检查宽度是否大于所需的宽度,如果是的话,以减少CSS的字体大小,并重新分析CSS并使其自动大小。但由于某种原因,它没有自动化,但是CSS正在被操纵。你知道我做错了什么吗?感谢您花时间帮助我!如何在css中动态设置文本大小与as3

package com.intentionally censored.view { 
import flash.display.MovieClip; 
import flash.text.TextField; 
import flash.text.StyleSheet; 
import com.intentionally censored.model.MatchupModel; 
import com.ff0000.ads.utils.Trace; 
import com.google.analytics.debug.Info; 

public class MatchupView extends MovieClip { 

    // on stage 
    public var maxTextFieldWidth = 150; 
    public var tf:TextField; 
    private var baseFontSize = 10; 
    public var $_tfCurrentWidth; 

    public function tfWidthController():void { 
     $_tfCurrentWidth = tf.textWidth; 
     trace('textfields current width is ' + $_tfCurrentWidth); 
     trace('textfields max width is ' + maxTextFieldWidth); 
     while ($_tfCurrentWidth > maxTextFieldWidth) { 
      trace ('$_tfCurrentWidth is '+$_tfCurrentWidth); 
      trace ('maxTextFieldWidth is '+maxTextFieldWidth); 
      baseFontSize-=1; 
      applyStyles(); 
     } 
    } 

    private function get styles():StyleSheet { 
     var _styles:StyleSheet = new StyleSheet(); 
     _styles.parseCSS("* {font-family:Playoff Bold Italic;font-style:boldItalic;}.em {font-size:"+(baseFontSize+2)+"px; }.redMicroTxt{font-size:"+baseFontSize+"pt;color:#c00000;}.medWhiteTxt{font-size:"+(baseFontSize+3)+"pt;color:#FFFFFF;}.redMediumTxt{font-size:"+(baseFontSize+6)+"pt;color:#c00000;}.whiteLargeTxt,.whiteMedTxt{font-size:"+(baseFontSize+6)+"pt;color:#FFFFFF;font-style:italic;}"); 
     return _styles; 
    } 

    public function MatchupView() { 
     tfWidthController(); 
     applyStyles(); 

    } 

    protected function applyStyles():void { 
     tf.styleSheet = styles; 
     tf.autoSize = "center"; 
    } 

    public function prepareFor($_matchupModel:Object):void { 
     Trace.out(this, 'prepareFor()'); 
     var _timeMessage:String = ''; 
     var _dateMessage:String = 'LIVE NOW'; // -------------- logic for 160 includes "\r" 
     trace($_matchupModel.gameStatus); 
     if($_matchupModel.gameStatus != 'Live Now') {    
      _timeMessage = $_matchupModel.timeMessage; 
      _dateMessage = 'PM/ET'; 
     } 


     tf.htmlText = '<span class="redMicroTxt">' + $_matchupModel.team1Rank + " "+'</span>'+ 
         '<span class="whiteLargeTxt">' + $_matchupModel.team1 + '</span>'+ 
         '<span class="medWhiteTxt"> vs </span>'+ 
         '<span class="redMicroTxt">' + $_matchupModel.team2Rank + " "+'</span>'+ 
         '<span class="whiteLargeTxt">' + $_matchupModel.team2 + '</span> '+ 
         '<span class="redMediumTxt">' + _timeMessage + 
         '<span class="redMicroTxt">' + _dateMessage + '</span></span>'; 
    } 

} 

}

+0

这是所有的代码? tf作为孩子添加到哪里,并且是分配的autosize属性? – Gone3d

+0

其链接到我的比赛中的文本字段。这是控制文本字段的唯一重要代码。如果问题在任何地方,它会在这里。它工作正常,如果我不动态改变通过while循环的风格。 –

回答

1

我想通了 - 你需要在每次调整时更改的htmlText。

while ($_tfCurrentWidth > maxTextFieldWidth) { 
    trace ('$_tfCurrentWidth is '+$_tfCurrentWidth); 
    trace ('maxTextFieldWidth is '+maxTextFieldWidth); 
    var oldHtml:String = tf.htmlText; 
    tf.htmlText = ''; 
    tf.htmlText = oldHtml; 
    baseFontSize-=1; 
    applyStyles(); 
} 

我也做这个工作的测试,你可以在这里看到:http://wonderfl.net/c/A310

相关问题