[Flash] setTextFormat() v.s. defaultTextFormat

  • setTextFormat() v.s. defaultTextFormat
  • 之前在使用TextField時常常被這兩個東西搞混,這裡做個小小紀錄。

    setTextFormat() :用在文字已經寫在text這個變數上時,範例如下:

    var txtFmt:TextFormat = new TextFormat();
    txtFmt.size = 16;
    txtFmt.bold = true;
    txtFmt.color = 0x0000ff;

    var txtField:TextField = new TextField();
    addChild(txtField);
    txtField.x = 100;
    txtField.y = 10;
    txtField.text = "setTextFormat method.";
    txtField.autoSize = TextFieldAutoSize.CENTER;
    txtField.setTextFormat(txtFmt);

    承接著上面的使用方式,如果要做後續的性質修改,必須使用下面的方法:

    txtFmt = txtField.getTextFormat();
    txtFmt.size = 28;
    txtField.setTextFormat(txtFmt);

    defaultTextFormat:用在文字是由執行時期所輸入的,範例如下:

    var txtFmt:TextFormat = new TextFormat();
    txtFmt.size = 16;
    txtFmt.bold = true;
    txtFmt.color = 0x0000ff;

    var txtField:TextField = new TextField();
    addChild(txtField);
    txtField.type = TextFieldType.INPUT;
    txtField.x = 100;
    txtField.y = 10;
    txtField.autoSize = TextFieldAutoSize.CENTER;
    txtField.defaultTextFormat = txtFmt;

    承接著上面的使用方式,如果要做後續的性質修改,必須使用下面的方法:

    txtFmt = txtField.defaultTextFormat;
    txtFmt.size = 28;
    txtField.defaultTextFormat = txtFmt;

    這時問題又來了,如果我又想要先預設一段文字,且又希望此TextField可以作為輸入,那你的程式要這樣寫:

    var txtFmt:TextFormat = new TextFormat();
    txtFmt.size = 16;
    txtFmt.bold = true;
    txtFmt.color = 0x0000ff;

    var txtField:TextField = new TextField();
    addChild(txtField);
    txtField.type = TextFieldType.INPUT;
    txtField.x = 100;
    txtField.y = 10;
    txtField.text = "editable TextField with setTextFormat.";
    txtField.autoSize = TextFieldAutoSize.CENTER;
    txtField.setTextFormat(txtFmt);

    ...@@

2 comments:

  1. hi xin yu

    thanks for making this clear. but i do wan to raise one thing up here in your blog.

    if you are using setTextFormat method and you wan to change the style (eg. fontsize), you have to retrieve the textfield's textformat using getTextFormat method and set it back using setTextFormat method.

    if you are using defaultTextFormat and you wan to change the style, you have to use back defaultTextFormat properties to retrieve the TextFormat and set it back using defaultTextFormat properties.

    using defaultTextFormat properties and setTextFormat method can never work together at the same time.

    hope this will help other who searching for solution.

    ReplyDelete
  2. Hello, Darien

    Thanks for telling us this technique. It would make this topic more complete.

    Thank you!

    ReplyDelete

Orange - data analysis tool

Installation pip install orange3 Run orange python -m Orange.canvas