AS3 缩放文本函数导致与文本字段 HTML 格式冲突
大家好,我正在使用下面的函数在运行时缩放动态字体大小,如下所示:
function scaleTextToFitInTextField( txt : TextField ):void{
var f:TextFormat = txt.getTextFormat();
f.size = ( txt.width > txt.height ) ? txt.width : txt.height;
txt.setTextFormat( f );
while ( txt.textWidth > txt.width - 4 || txt.textHeight > txt.height - 6 ){
f.size = int( f.size ) - 1;
txt.setTextFormat( f );
}
}
scaleTextToFitInTextField( tf );
// tf is a dynamic multiline textfield on stage with dimension 150x150
这个想法是,当文本字段填充外部内容时,它会减小字体大小以使所有文本适合文本字段。这项工作到目前为止。
我现在最大的问题是这个函数会干扰文本字段 html 格式。例如;我加载外部 html:
<font size="-2">this text is way</font><br><font size="+5">TOO BIG</font><br>to fit in this box, but I'll give it a try!
将缩放函数应用于文本字段时,html 格式(大小变化)不起作用,但如果我删除缩放函数,它就会起作用。
理想情况下,如果文本太多而无法容纳,我希望能够缩放字体,并且也能够应用 html 格式。
请有人帮忙。我正在使用 CS5
非常感谢。
Hello Guys I am using the below function to scale dynamic font size at run time as thus:
function scaleTextToFitInTextField( txt : TextField ):void{
var f:TextFormat = txt.getTextFormat();
f.size = ( txt.width > txt.height ) ? txt.width : txt.height;
txt.setTextFormat( f );
while ( txt.textWidth > txt.width - 4 || txt.textHeight > txt.height - 6 ){
f.size = int( f.size ) - 1;
txt.setTextFormat( f );
}
}
scaleTextToFitInTextField( tf );
// tf is a dynamic multiline textfield on stage with dimension 150x150
The idea is that when the textfield is populated with external content, it reduces the font size to fit all the text into the texfield. This work so far.
My biggest problem now is that this function interferes with the textfield html formatting. For instance; I Load an external html:
<font size="-2">this text is way</font><br><font size="+5">TOO BIG</font><br>to fit in this box, but I'll give it a try!
With the scale function applied to the textfield, the html formatting (size variations) does not work, but if I remove the scale function, it does.
Ideally, I want to be able to scale the font if text is too much to fit and be able to apply html format as well.
Somebody pls help. I am using CS5
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将 htmlText 包装在
中,它可能会起作用:
否则,您可以尝试将 TextField 包装在 Sprite 中并缩放 Sprite? 如何做您使用as3从中心点缩放flash中的动态文本字段吗?
It may work if you wrap the htmlText in
<![CDATA[]]>
:Otherwise, you could try wrapping the TextField in a Sprite and scale the Sprite instead? How do you scale a dynamic textfield in flash from the center point using as3?