【问题标题】:HTML Tags Don't Work When Text Resized调整文本大小时 HTML 标记不起作用
【发布时间】:2012-07-21 21:32:14
【问题描述】:

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

【问题讨论】:

    标签: html flash actionscript resize textfield


    【解决方案1】:

    尝试将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);
    

    【讨论】:

    • 255 是文本框的高度。
    • 255 是文本框的高度。我的问题是文本框中可能有太多文本无法容纳,因此会调整其大小。发生这种情况时,HTML 标记不起作用。在您的示例中,文本不会换行,因此当您向字符串添加更多内容时,它会中断。我的文本框是 455.40W x 255H。
    • 我的错,我弄错了TextField.textHeight。无论如何,我使用 wordwrap 修复了我的简单代码示例,它在调整大小和不需要时都很好。请确保使用 desc_txt.defaultTextFormat = fmtD;而不是 setTextFormat(fmtD);每次。
    • 谢谢!尝试了几次,但我终于能够将您的代码应用到我的代码中。
    猜你喜欢
    • 1970-01-01
    • 2016-04-20
    • 2014-11-14
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 2018-12-28
    • 1970-01-01
    相关资源
    最近更新 更多