【问题标题】:Why is TextFormat not working on my TextArea?为什么 TextFormat 在我的 TextArea 上不起作用?
【发布时间】:2016-12-07 04:10:17
【问题描述】:

我试图在单击文本区域后生成粗体文本。我做错了什么?

HelloWorld.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" width="800" height="800">
    <mx:Script source="HelloWorldAS.as" />

        <mx:VBox width="70%" height="70%" label="Container">
            <mx:TextArea id="lblTest"  verticalScrollPolicy="off" focusThickness="0" borderThickness="0" borderStyle="none" editable="true" fontFamily="Arial" fontSize="14" width="100%" height="100%" click="areaClick()"/>
        </mx:VBox>

</mx:Application>

HelloWorldAS.as

// ActionScript file
import flash.text.TextField;
import flash.text.TextFormat;

public function areaClick() : void{
    lblTest.text = "Hello world!";

    var format:TextFormat = new TextFormat();
    format.bold=true;

    lblTest.setStyle("textFormat", format);
    lblTest.validateNow();
}

【问题讨论】:

    标签: actionscript-3 apache-flex actionscript adobe


    【解决方案1】:

    很遗憾,TextArea 没有 textFormat 样式。使用fontWeight 作为bold 如下:

    lblTest.setStyle("fontWeight", "bold");
    

    【讨论】:

      【解决方案2】:

      您可以在此处阅读官方 adobe 文档:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextFormat.html,您最感兴趣的部分是:

      使用 TextField.defaultTextFormat 属性应用格式 在将文本添加到 TextField 和 setTextFormat() 方法之前 在向 TextField 添加文本后添加格式。

      因此,如果您希望文本“hello world”为粗体,则必须按如下方式应用 TextFormat:

      lblText.setTextFormat(format);
      

      【讨论】:

      • 谢谢,但我将它用于 TextArea 而不是 TextField。我尝试设置 lblTest.text = "Hello world!"在我设置格式属性之后它仍然没有显示出来。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多