2012-12-03 4 views
1

현재 기본 글꼴 크기 변수를 통해 전역으로 스타일 시트를 기반으로 텍스트 크기를 동적으로 설정하려고합니다. 나는 현재 tfWidthController 메서드를 사용하여 너비가 원하는 너비보다 큰지 확인하고 css에서 글꼴 크기를 줄이고 css를 다시 구문 분석하고 자동 크기를 지정합니다. 그러나 어떤 이유로 그것의 autosizing하지 않지만 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

관련 문제