正如你所说,getTextFormat(beginIndex:int = -1, endIndex:int = -1) 只返回一个 TextFormat 对象,其中填充了指定文本范围的格式信息(只有指定的整个文本共有的属性才会在结果 TextFormat 对象中设置)。
如果要全文的格式信息,请在调用setTextFormat后读取TextField的htmlText属性。它包含文本字段内容的 HTML 表示。
var str:String = "The quick brown fox jumps over the lazy dog\n";
var tf:TextField = new TextField();
tf.multiline = true;
tf.text = str + str + str;
var f:TextFormat = new TextFormat("Arial", 15, 0xFF0000, true, false,
true, null, null, TextFormatAlign.CENTER);
tf.setTextFormat(f, 0, str.length);
f = new TextFormat("Courier New", 12, 0x00FF00, false, false,
false, "http://www.google.com", null, TextFormatAlign.LEFT);
tf.setTextFormat(f, str.length, 2 * str.length);
f = new TextFormat("Times New Roman", 12, 0x0000FF, false, true,
true, null, null, TextFormatAlign.RIGHT);
tf.setTextFormat(f, 2 * str.length, str.length * 3);
tf.width = 400;
tf.height = 300
addChild(tf);
trace(tf.htmlText);
输出是:
<P ALIGN="CENTER">
<FONT FACE="Arial" SIZE="15" COLOR="#FF0000" LETTERSPACING="0" KERNING="0">
<B>
<U>The quick brown fox jumps over the lazy dog</U>
</B>
</FONT>
</P>
<P ALIGN="LEFT">
<FONT FACE="Courier New" SIZE="12" COLOR="#00FF00" LETTERSPACING="0" KERNING="0">
<A HREF="http://www.google.com" TARGET="">The quick brown fox jumps over the lazy dog</A>
</FONT>
</P>
<P ALIGN="RIGHT">
<FONT FACE="Times New Roman" SIZE="12" COLOR="#0000FF" LETTERSPACING="0" KERNING="0">
<I>
<U>The quick brown fox jumps over the lazy dog</U>
</I>
</FONT>
</P>