开发者

Formatted text in flash

开发者 https://www.devze.com 2023-03-12 04:29 出处:网络
Is it possible to formatted text or a formatted text document (. doc) to bring开发者_运维技巧 into flash to TextFieldTextField supports some HTML formatting. You need to use htmlText property for that

Is it possible to formatted text or a formatted text document (. doc) to bring开发者_运维技巧 into flash to TextField


TextField supports some HTML formatting. You need to use htmlText property for that. It supports bold, italic, font etc. But font needs to be available in system or embedded. And img tag is not fully supported.


There are some things that a TextField can't do. One thing for sure is you can't change the background color on a per character basis. But really you can do a ton more formatting then people obviously think.

This is a VERY quick and dirty prototype to show you how it basically works.

package src 
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public class Main extends Sprite 
    {
        private var tf:TextField;
        private var tform:TextFormat;
        private var iForm:TextFormat;

        public function Main() 
        {
            addEventListener(Event.ADDED_TO_STAGE, initMain);
        }

        private function initMain(e:Event):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, initMain);

            tform = new TextFormat("Arial", 12, 0);
            iForm = new TextFormat("Arial", 12, 0xff0000, true, true);

            tf = new TextField();
            tf.defaultTextFormat = tform;
            tf.text = "A sentence is just a sentence.";
            tf.x = 100;
            tf.y = 100;
            tf.width = 300;
            addChild(tf);

            tf.setTextFormat(iForm, 2, 10);

            // We can even change the TextFormat we used just one line above and 
            // reuse it for a completely different format. Finding out the starting
            // and ending indexes isn't hard either. There are a few options. RegEx being my go to guy.
            iForm.color = 0x0000ff;
            iForm.size = 20;
            iForm.italic = false;
            tf.setTextFormat(iForm, 20, 29);
            tf.appendText(" And yet some added text doesn't mess it up!");
        }
    }
}

hope this helps!

If you need more than what a TextField can give you, your going to have to go with TLF. (which rocks also.)


You can't do it directly from a .doc file. You can do it using a limited subset of HTML. TextField.htmltext supports

< a > links

< b > bold

< i > italics

< u > underline

< img > images inline

< font face="Arial" size="18" color="#FFCC00"> Font face tags.

< ul >< li > unordered lists (bullet points)

So you can import formatted text if your formatting uses the above html tags. If you are bringin in the text from XML - it is often useful to wrap the imported text in a tag to avoid escaping characters such as "<" and "&".

0

精彩评论

暂无评论...
验证码 换一张
取 消