使用适合文本动态适应宽度flex 4.6的百分比

时间:2012-08-29 11:21:03

标签: css actionscript-3 flex dynamic text

flex 4.6

我有一个模块,其中包含一个带有标签的边框容器。我正在动态加载文本,并希望文本将标签填充为100%宽度。我在下面找到了对fitText函数的引用,但不知道如何/在哪里包含这段代码。

我也试过用css。我可以使用填充和放大器吗?标签的边距,以获得文本的百分比宽度?

如果没有,有人可以提供代码如何包含/调用该函数。

我收到了几个错误/?在下面的fitText中,例如

Access of undefined property FormattedTextField
Access of undefined property autoSize
Access of undefined property text
Access of undefined property width
Access of undefined property width
Access of undefined property numLines

thx

技术

模块

<s:BorderContainer x="27" y="19" width="404" height="67" backgroundColor="#111">
        <s:Label id="hlText" x="54" y="5" width="307" height="55"  fontSize="55" styleName="HeadBanner"
                 text="{Mytext.heading}"/>
    </s:BorderContainer> 
根据{{​​3}}

fitText

package com.v1
{
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
import flash.text.TextLineMetrics;
import flash.text.TextField;


/**
 * Changes the size of the text to fit a given width and number of lines.
 * Useful for news headlines that should extend across a full column.
 *
 * We need to know:
 * - which font family, weight, and style to use
 * - the max width of the headline
 * - the max height in pixels of the headline
 * - the max number of lines
 * 
 * Algorithm 1:
 * - figure out the N-width of a character that should work based on pixelWidth / numChars
 * - translate that N-width into a point size
 * - try the point size, if outside of tolerance,; 
 *   - if too wide, adjust down a point, try again
 *   - if too small, adjust up a point, try again
 *   - if too wide last time, too small this time or vice versa, stick with the too small size
 * 
 * Pixels per character (width-wise) is roughly 1/2 the point size, so that's a good starting
 * point. So to get a starting point size, divide the overall width by the number of characters
 * to get pixels-per-character, then double to get the point size.
 */
public class fitText extends FormattedTextField
{

    public static var MIN_POINT_SIZE:uint = 6;
    public static var MAX_POINT_SIZE:uint = 128;

    public function HeadlineTextField(tf:TextFormat = null) 
    {
        super(tf);
        this.autoSize = TextFieldAutoSize.LEFT;
    }

    public function fitText(msg:String, maxLines:uint = 1, toUpper:Boolean = false, targetWidth:Number = -1):uint
    {
        this.text = toUpper ? msg.toUpperCase() : msg;

        if (targetWidth == -1)
        {
            targetWidth = this.width;
        }

        var pixelsPerChar:Number = targetWidth / msg.length;

        var pointSize:Number = Math.min(MAX_POINT_SIZE, Math.round(pixelsPerChar * 1.8 * maxLines));

        if (pointSize < 6)
        {
            // the point size is too small
            return pointSize;
        }

        this.changeSize(pointSize);

        if (this.numLines > maxLines)
        {
            return shrinkText(--pointSize, maxLines);
        }
        else
        {
            return growText(pointSize, maxLines);
        }
    }

    public function growText(pointSize:Number, maxLines:uint = 1):Number
    {
        if (pointSize >= MAX_POINT_SIZE)
        {
            return pointSize;
        }

        this.changeSize(pointSize + 1);

        if (this.numLines > maxLines)
        {
            // set it back to the last size
            this.changeSize(pointSize);
            return pointSize;
        }
        else
        {
            return growText(pointSize + 1, maxLines);
        }
    }

    public function shrinkText(pointSize:Number, maxLines:uint=1):Number
    {
        if (pointSize <= MIN_POINT_SIZE)
        {
            return pointSize;
        }

        this.changeSize(pointSize);

        if (this.numLines > maxLines)
        {
            return shrinkText(pointSize - 1, maxLines);
        }
        else
        {
            return pointSize;
        }
    }
}
}

0 个答案:

没有答案