0

我有一个动态填充的文本字段。如果填充的文本不适合文本字段,则会调整文本大小。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;
    }    
} 
4

1 回答 1

0

尝试将while 循环中的最后两行更改为:

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

您可以在此处阅读有关使用 html 标记和 as3 TextFormat 类的更多信息

编辑:

我只是使用您的代码设置了这个简单的示例,并且文本格式(字体类型和大小)和 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);
于 2012-07-23T16:37:41.090 回答