【问题标题】:Flash - can't get TextLayoutFormat of TLFTextFieldFlash - 无法获取 TLFTextField 的 TextLayoutFormat
【发布时间】:2011-11-04 08:01:00
【问题描述】:

我要做的就是获取 TLFTextField 的格式属性并将其应用到另一个 TLFTextField。使用经典的 TextField 很简单:

var textFormat:TextFormat = text1.getTextFormat();
text2.setTextFormat(textFormat);

TLFTextField 有一个 getTextFormat 和 setTextFormat 函数,但它们都非常有问题。 getTextFormat 仅在您将 selectable 属性更改为 true 时才有效,否则会生成空对象错误。当 TextFormat 对象的某些属性不为空时,setTextFormat 会生成 NaN 错误。

TextLayoutFormat 对象应该用于 TLFTextFields。您可以通过执行以下操作来设置对象:

var text1:TLFTextField = new TLFTextField();
var textLayoutFormat:TextLayoutFormat = new TextLayoutFormat();
var textFlow:TextFlow = text1.textFlow;
textFlow.hostFormat = textLayoutFormat;
textFlow.flowComposer.updateAllControllers();

但是,我现在无法弄清楚如何从 text1 中“获取”TextLayoutFormat。有人提出以下建议:

textLayoutFormat = ((text1.textFlow.getChildAt(0) as ParagraphElement).getChildAt(0) as SpanElement).computedFormat as TextLayoutFormat;

但这只是返回 null。有谁知道如何获取 TextLayoutFormat 以便我可以将其应用于另一个 TLFTextField?

【问题讨论】:

    标签: flash actionscript-3 textfield tlf


    【解决方案1】:

    您使用的是 Flex 还是 Flash?我最近对 ​​TLF 感到头疼。这个页面对我来说是一个很好的参考:http://flashthusiast.com/2010/05/05/getting-started-with-the-tlftextfield-class-in-actionscript-3-0-and-flash-cs5/你是在文本之前设置文本格式吗?我已经这样做了,没有遇到你的 null 问题。

    【讨论】:

    • 我正在使用使用 Flex SDK 的 FlashDevelop。是的,我在设置文本之前设置了文本格式。我看过那篇文章,他们使用 TextFormat 对象而不是 TextLayoutFormat。当 TextFormat 参数的某些属性不为空时,setTextFormat 似乎不喜欢它,例如行间距。
    • 好的,当我运行 getTextFormat 时,返回的 TextFormat 的属性 'leading' 是一个奇怪的负数,看起来像 min int,而属性 'letterSpacing' 是 NaN。这些会导致 setTextFormat 崩溃。如果我在运行 setTextFormat 之前将这些属性设置为 null,一切都很好,但显然它没有得到我需要的前导和字母间距。我想我会为此提出一个新问题,因为这是一个不同的问题。
    【解决方案2】:

    在简单的情况下这是可行的

    var format:TextLayoutFormat = textField.textFlow.hostFormat as TextLayoutFormat;
    format.fontFamily = fontFamily;
    textField.textFlow.hostFormat = format;
    textField.textFlow.flowComposer.updateAllControllers();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      相关资源
      最近更新 更多