找到实际文本高度AS3

use*_*515 10 actionscript-3

如何获取AS3 TextField中文本的实际高度?TextField.textHeight似乎报告了一些不依赖于TextField内容的固定值.

以下示例代码生成以下内容:

text=o p g y d j
textWidth=120.8
textHeight=**96**

text=o
textWidth=15
textHeight=**96**

text=oW
textWidth=43.3
textHeight=**96**
Run Code Online (Sandbox Code Playgroud)

显然,"o"和"p"等的高度应该不同.

AS3代码:

import flash.text.TextField;

        var format : TextFormat = new TextFormat();
        format.font = "Times New Roman";
        format.size = 30;
        format.align = TextFormatAlign.CENTER;


        var textField1 : TextField = new TextField();       
        textField1.defaultTextFormat = format;      

        textField1.selectable = false;              
        textField1.sharpness = 0;   
        textField1.embedFonts = true;   
        textField1.multiline = false;

        textField1.height = 50;
        textField1.width = 200;
        textField1.x = 10;
        textField1.y = 10;

        addChild(textField1);

        textField1.text = "o p g y d j";    
        trace("text=" + textField1.text);
        trace("textWidth=" + textField1.textWidth);
        trace("textHeight=" + textField1.textHeight);       

        textField1.text = "o";      
        trace("\ntext=" + textField1.text);
        trace("textWidth=" + textField1.textWidth);
        trace("textHeight=" + textField1.textHeight);   

        textField1.text = "oW";     
        trace("\ntext=" + textField1.text);
        trace("textWidth=" + textField1.textWidth);
        trace("textHeight=" + textField1.textHeight);       

        stop();
Run Code Online (Sandbox Code Playgroud)

我猜TextField.textHeight不是正确的变量,但我应该使用什么呢?

Jim*_*iTh 13

Mark Fox是正确的,textHeight它不代表文本的实际高度 - 而TextFieldFlash中的经典对于获取渲染文本的实际像素高度没有任何支持.什么textHeight 表示是线高度-这是它的上升(字体的高度高于基线),血统(字体 '低于基线的身高)和龙头(线之间的空间)相结合.如所暗示的,高度是恒定的,基于字体的上升和下降,而不是实际的文本.(请记住这一点,请参阅Adobe对此处术语的概述 - 并注意对TextLineMetrics您也没有帮助).

"新"Flash文本引擎(flash.text.engine)确实包含了获取使用该技术渲染的文本的实际高度的属性(例如TextLine.totalHeight) - 但随后我们将进入低级文本渲染.如果您需要使用"经典" TextField,则无论如何都无法帮助您测量文本,因为Flash文本引擎具有自己的渲染器,渲染器不一定以与"经典"文本相同的高度和宽度渲染文本.

你可以做的是渲染TextField,BitmapData然后测量文本的边界:

// Create a completely transparent BitmapData:
var bmd:BitmapData = new BitmapData(
                            textfield.width, 
                            textfield.height, 
                            true,
                            0x00ffffff);

// Note that some cases may require you to render a Sprite/MovieClip
// CONTAINING the TextField for anything to get drawn.
// For example, AntiAliasType.ADVANCED (antialias for readability) is known to 
// not be renderable in some cases - other settings may cause nothing to be
// rendered too. In that case, simply wrap add the TextField as a child of an 
// otherwise empty Sprite/MovieClip, and pass that to draw() instead:
bmd.draw(textfield);

// This gets the bounds of pixels that are not completely transparent.
// Param 1: mask  = specifies which color components to check (0xAARRGGBB)
// Param 2: color = is the color to check for.
// Param 3: findColor = whether to bound pixels OF the specified color (true),
//                      or NOT OF the specified color (false)
//
// In this case, we're interested in:
// 1: the alpha channel (0xff000000)
// 2: being 00 (0x00......)
// 3: and want the bounding box of pixels that DON'T meet that criterium (false)
var rect:Rectangle = bmd.getColorBoundsRect(0xff000000, 0x00000000, false);

// Do remember to dispose BitmapData when done with it:
bmd.dispose();

trace("text height = " + rect.height);
trace("text width = " + rect.width);
Run Code Online (Sandbox Code Playgroud)

注意准确性

这可能完全不相关,取决于您将要使用的内容,但值得记住:

这种方法显然总是在整个像素中返回结果 - 然而,字形的实际渲染使用子像素.

换句话说,如果添加测量"V"和"o"宽度的结果,然后将其与"Vo"的结果进行比较,它们可能不相同.你可能得到"40 + 35 = 74".忽略字距调整等可推动信件更靠近在一起,每个字母(放置,抗混叠等)的呈现也可以是不同的,这取决于它的上下文.

  • 您知道Adobe已经在新的Flash CC版本中弃用了TLF,对吗?http://help.adobe.com/en_US/flash/cs/using/WSb03e830bd6f770ee-4b0db644124bbdb363d-8000.html (3认同)
  • 在创建`bmd`时,你将`fillColor`设置为`0xffffffff`,用纯白色填充它.既然你想要它完全透明,也许你的意思是`0x00ffffff`(尽管只有'0`很好). (2认同)