A Flash Developer Resource Site

Results 1 to 13 of 13

Thread: Change color of certain words within dynamic textbox..

  1. #1
    Member
    Join Date
    Oct 2008
    Posts
    37

    Change color of certain words within dynamic textbox..

    Is this possible, cos all my attempts havent worked..
    here is what i have:
    Actionscript Code:
    function colorChange(field,findstring) {
        var tf = new TextFormat();
        tf.color = 0x800080;
        var pos = field.text.indexOf(findstring);
        field.setTextFormat(tf, pos, pos + findstring.length);
    }

    called via:
    Actionscript Code:
    colorChange(_root.dynamicField, "word");

    I just want the word 'word' to be #800080 within the dynamic textbox labelled 'dynamicField'

    Any ideas?

  2. #2
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Hi,

    I did not write all of this but it is what you require

    Actionscript Code:
    field.text = "One day this word will be seen in a different colour. The next word will not be the same as the first word but a different word.";

    function SetColor(A, B, Color)
    {
        var NewColor = new TextFormat();
        NewColor.color = Color;

        for (i = 0; i < field.text.length; i++)
        {
            if (field.text.substr(i, A.length + B.length) == A + B)
            {
                field.setTextFormat(i,i + A.length,NewColor);
            }
        }
    }

    function NewColor(Color)
    {
        field.textColor = 0x000000;
    }

    NewColor();

    SetColor("word"," ",0x800800);
    SetColor("same"," ",0xFF0000);
    SetColor("different"," ",0x00FF05);

    toodles

  3. #3
    Member
    Join Date
    Oct 2008
    Posts
    37
    thanks
    Last edited by mexicanbean; 07-15-2012 at 04:10 PM.

  4. #4
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    and if you want to change the size of various words too then put this underneath the colour change code.

    Actionscript Code:
    function SetSize(A, B, textSize)
    {
        var NewSize = new TextFormat();
        NewSize.size = textSize;

        for (i = 0; i < field.text.length; i++)
        {
            if (field.text.substr(i, A.length + B.length) == A + B)
            {
                field.setTextFormat(i,i + A.length,NewSize);
            }
        }
    }

    SetSize(field.text,"",11);
    SetSize("One","",30);
    SetSize("same","",14);
    SetSize("different","",22);
    SetSize("seen","",20);
    SetSize("colour","",16);
    SetSize("completely","",40);

  5. #5
    Member
    Join Date
    Oct 2008
    Posts
    37
    It works good, but it wont change any words that are tags such as <tag> it will only change actual words.

    Any idea why?

  6. #6
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    it will if you make your text field html false
    but then you will lose some formatting

    or you can try to use hex codes

    %3C = <
    %3E - >
    Last edited by fruitbeard; 07-15-2012 at 04:39 PM.

  7. #7
    Member
    Join Date
    Oct 2008
    Posts
    37
    do u mean the button that says "render text as html" cos if so i already have that unchecked...?

  8. #8
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    try it and see, but i think so yes

    or post your code or fla along with the text you wish to use

  9. #9
    Member
    Join Date
    Oct 2008
    Posts
    37
    Here is the .fla, u will see that its all working but it wont color the tags...
    (im using flash 8 so if you re-save it make sure i can open it )
    Attached Files Attached Files

  10. #10
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    HI, ok then, i use CS6 but it will be ok, i'll jujst give you the code

    slightly different

    Actionscript Code:
    fetchit = new LoadVars();
    fetchit.load("mytext.txt",_root);
    fetchit.onLoad = function(success)
    {
        if (success)
        {
            field1.htmlText = this.myText;
            SetColorMain(field1.htmlText,"",0x666666);
            SetColorMain("<title>","",0x800800);
            SetColorMain("</title>","",0x800800);
            SetColorMain("test","",0xFF0000);
            SetColorMain("This","",0x00FF05);
        }
        else
        {
            field1.htmlText = "Unable to find the text file";
        }
    };

    function SetColorMain(A, B, Color)
    {
        var NewColor = new TextFormat();
        NewColor.color = Color;

        for (i = 0; i < field1.text.length; i++)
        {
            if (field1.text.substr(i, A.length + B.length) == A + B)
            {
                field1.setTextFormat(i,i + A.length,NewColor);
            }
        }
    }

    now copy the next code into a txt file and save it as mytext.txt, keep in same directory as swf.

    loading the text data externally so you can easily change it

    Code:
    &myText=<title>This is also a test </title> tags should be purple.
    Last edited by fruitbeard; 07-15-2012 at 05:17 PM.

  11. #11
    Member
    Join Date
    Oct 2008
    Posts
    37
    That wont work, i dont want to use text files, the text that i am wanting to format is generated so text files wouldnt work.

    Is there any particular reason why tags dont work?

  12. #12
    . fruitbeard's Avatar
    Join Date
    Oct 2011
    Posts
    1,780
    Actionscript Code:
    field1.htmlText = "<title>This is also a test </title> tags should be purple.";
    function SetColorMain(A, B, Color)
    {
        var NewColor = new TextFormat();
        NewColor.color = Color;

        for (i = 0; i < field1.text.length; i++)
        {
            if (field1.text.substr(i, A.length + B.length) == A + B)
            {
                field1.setTextFormat(i,i + A.length,NewColor);
            }
        }
    }

    SetColorMain(field1.text,"",0x666666);
    SetColorMain("<title>","",0x800800);
    SetColorMain("</title>","",0x800800);
    SetColorMain("test","",0xFF0000);
    SetColorMain("This","",0x00FF05);

  13. #13
    Member
    Join Date
    Oct 2008
    Posts
    37
    yeah thats working properly now, thanks for the help fruitbeard

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center