2012-07-23 63 views
0

我有一個動態填充的文本字段。如果填充的文本不適合文本字段,則文本會被調整大小。 HTML標籤可以在動態填充的文本中正常工作,但前提是文本不會被調整大小。 在調整大小的文本中忽略HTML標記。任何想法?調整文本大小時HTML標記不起作用

代碼文本字段:

import flash.text.TextFormat; 
    import flash.text.Font; 
    // 
    function setDesc(str:String):void{ 
     var fmtD:TextFormat; 
     var cfmtD:TextFormat = this.desc_txt.getTextFormat()==null ? this.desc_text.defaultTextFormat : this.desc_txt.getTextFormat(); 
     var sizeD:int = 22; 
     desc_txt.htmlText = str; 
     while(sizeD>10 && sizeD<23 && desc_txt.textHeight>255){ 
     sizeD--; 
     fmtD = new TextFormat(descFont.fontName,sizeD,0x000000,false, false,false); 
     desc_txt.htmlText = str; 
     desc_txt.setTextFormat(fmtD); 
     } 
} 

代碼填充文本字段:

function openDialog(e:MouseEvent){ 
    dialog_window.doOpen(); 
     switch(e.currentTarget.name){ 
     case "btn_structure": 
      dialog_window.setTitle("Business Structure:"); 
      dialog_window.setDesc("This topic discusses the <b>basic</b> structure of the business area."); 
     break; 
     case "btn_services": 
      dialog_window.setTitle("Services Provided:"); 
      dialog_window.setDesc("This topic provides <i>information</i> about the services offered by the Client Billing Services unit."); 
     break; 
    }  
} 

回答

0

嘗試最後兩行改變while循環裏面:

desc_txt.defaultTextFormat = fmtD; 
desc_txt.htmlText = str; 

你可以閱讀更多關於working with html tags and the as3 TextFormat class here

編輯:

我只是設置使用你的代碼,無論是文本格式(字體類型和大小)和HTML標記的比特這個簡單的例子(大膽斜體)正在工作完美:

import flash.text.TextField; 
import flash.text.TextFormat; 

var sizeD:uint = 22; 
var desc_txt:TextField = new TextField(); 
addChild(desc_txt); 

var str:String = "This topic <i>discusses</i> the <b>basic</b> structure of the business area. This topic <i>discusses</i> the <b>basic</b> structure of the business area.This topic <i>discusses</i> the <b>basic</b> structure of the business area. This topic <i>discusses</i> the <b>basic</b> structure of the business area."; 

var fmtD:TextFormat; 
fmtD = new TextFormat("Verdana",sizeD,0x000000,false, false,false); 

desc_txt.width=450; 
desc_txt.height=150; 
desc_txt.wordWrap=true; 

desc_txt.defaultTextFormat = fmtD; 
desc_txt.htmlText = str; 

var maxHeight = 150; 

trace(sizeD+" - "+ desc_txt.textHeight); 
while(sizeD>10 && sizeD<23 && desc_txt.textHeight>maxHeight) 
{ 
    trace("--> inside while loop"); 
    sizeD--; 
    fmtD = new TextFormat("Verdana",sizeD,0x000000,false, false,false); 
    desc_txt.defaultTextFormat = fmtD; 
    desc_txt.htmlText = str; 

} 
trace(sizeD+" - "+desc_txt.textHeight); 
+0

在這種情況下,HTML標記工作,但調整大小不。 – 2012-07-23 16:49:56

+0

奇怪,請檢查我的編輯,看看它是否可以幫助你... – danii 2012-07-23 17:09:09

+0

255是文本框的高度。 – 2012-07-23 17:24:55