代码之家  ›  专栏  ›  技术社区  ›  Steve

动态文本-自动调整字体大小

  •  2
  • Steve  · 技术社区  · 15 年前

    我的动态文本字段必须是固定的宽度和高度。

    将填充动态文本字段的实际文本是变量。

    如果文本没有完全显示在文本字段的维度中,我想做的是减小字体大小。

    我对如何准确执行这个有什么想法吗?

    另外,我使用的是2。

    谢谢

    2 回复  |  直到 15 年前
        1
  •  3
  •   Circuit in the wall    15 年前

    这应该有效:

    function updateFontSize(tField:TextField, defaultSize:Number) {
    var tFormat:TextFormat = new TextFormat();
    tFormat.size = defaultSize;
    tField.setTextFormat(tFormat);
    
    var size:Number = defaultSize;
    while((tField.textWidth > tField._width || tField.textHeight > tField._height) && size > 0) {
        size = size - 1;
        tFormat.size = size;
        tField.setTextFormat(tFormat);
    }}
    

    每当更改文本时调用此函数。此函数的第一个参数是文本字段。第二种是您喜欢的字体大小(如果太大,会减小)。

        2
  •  1
  •   PatrickS    15 年前

    遵循somebloke的代码,这里有另一种使用缩放的方法

    //set the TextField width & height 
    var fixedWidth:Number = 200;
    var fixedHeight:Number = 24;
    
    function scaleTextToSize(tField:TextField, defaultScale:Number) 
    {
       //You can fine tune the amount of scaling here
       var amount:Number = .1;
       var scale:Number = defaultScale;
    
       while((tField.width > fixedWidth || tField.height > fixedHeight) ) 
       {
          scale -= amount;
          tField.scaleX = tField.scaleY = scale;
       }
    }
    
    推荐文章