Anyway to Dynamically Change the Color of Text

Ask a Question related to Macromedia Flex General Discussion, Design and Development.

  1. #1

    Default Anyway to Dynamically Change the Color of Text

    Hello Forum Folks,

    I'm attempting to change the color of some text depending on the value of it's
    content, however when I try this, I get an error:
    Error: Binding is not allowed to style property color

    from this code:

    <mx:Text text="{rp.currentItem.qualificationStatus}" width="100"
    color="{chooseColor(rp.currentItem.qualificationSt atus)}" />

    Is there any object that allows for dynamic color changes to the text? Am I
    going about this the wrong way?

    Thanks for your help
    --Andy


    awclemen Guest

  2. Similar Questions and Discussions

    1. change text color
      Is there anyway in acrobat, to open a pdf of many pages and globally change the color of all the text? Is there anyway to do this in any program...
    2. Change ErrorMessage Text Dynamically
      Is there a way to set/change the ErrorMessage and/or Text property of validationcontrols without making a trip to the server? What I have so far...
    3. Can you change the color of text with the Touchup Text tool?
      I just want to say that your note helped me. I NEVER knew that I could use a right click to bring up properties when using the text touch-up tool!...
    4. How to change text color in 7 Pro?
      I am creating a PDF with many links. In 5 I would make the linked text blue so people would know it was a link. In 7 I could not do that, so I loaded...
    5. dynamically change of the background color
      Hi All. I am new to this forum and I am seeking help with an issue I have recently encountered. Here is the context of my problem: I have an...
  3. #2

    Default Re: Anyway to Dynamically Change the Color of Text

    Did you solve this?

    If not, I'll give it a shot.

    Tracy
    ntsiii Guest

  4. #3

    Default Re: Anyway to Dynamically Change the Color of Text

    yes and no. you cannot bind styles in Flex. Best (well, maybe not best, but
    easiest) thing to do is make a custom component that extends mx.controls.Text
    and then based on a setter method or property have a function execute to change
    the TextFormat of the text_mc ("text_mc" I think is the actual TextField
    instance inside of the Text class) or whatever. The reason I am suggesting
    this is because I needed a custom cell renderer that did something similar
    (change text color based on the value of the text) for a data grid. However,
    there's no reason why the component I made for the cell renderer could not be
    used as a normal alternative to the mx.controls.Text.

    Also, I think there is a method for setStyle on UIComponents that you might
    want to look into. That might get you on the right track for an easier
    alternative way to change style properties dynamically (since binding isn't
    allowed).

    jpwrunyan Guest

  5. #4

    Default Re: Anyway to Dynamically Change the Color of Text

    One of my apps really relies on color-coding, so i had to come up with
    something to do this in many places. Here is what i did:
    I created a .as file containing my function. I named it colorFormatter.as, and
    it looks like this:

    function colorMyLabel(myColor, myLabel):Void
    {
    //mx.controls.Alert.show(myColor + " <--myColor, " + myLabel + " <--myLabel");
    if(myColor == "1")
    {
    myLabel.setStyle("color","blue");
    }
    else if(myColor == "2")
    {
    myLabel.setStyle("color","green");
    }
    else if(myColor == "3")
    {
    myLabel.setStyle("color","#FFCC00");
    }
    else if(myColor == "4")
    {
    myLabel.setStyle("color","red");
    }
    else
    {
    myLabel.setStyle("color","black");
    }
    }

    You can use this function like this:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml">
    <Script source="colorFormatter.as"/>

    <mx:Label id="appLabel" text="Hello World!" initialize="colorMyLabel("2",
    this.appLabel)"/>

    </mx:Application>

    I do not know if this is the best way to do it, but I have found this very
    useful as far as reuse goes.

    Hope this helped.

    higgins1b Guest

  6. #5

    Default Re: Anyway to Dynamically Change the Color of Text

    Yeah, that was sort of what I was thinking except, if I were you I would make
    the .as script file into an .as class file. You could do something like:

    class ColorText extends mx.controls.Text {

    public var myColor:String;

    function draw() {
    super.draw();
    if (myColor !== undefined) {
    this.setStyle("color",myColor);
    }
    }

    }

    jpwrunyan Guest

  7. #6

    Default Re: Anyway to Dynamically Change the Color of Text

    sorry, forgot to add:

    in the mxml file you could do:
    <mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml"
    xmlns:as="*">

    <ColorText text="myText" myColor="0x00FF00" />

    </mx:Application>

    one more thing, I just noticed that you are using label while I used Text..
    but the basic implementation is the same. I haven't tested this code but I
    think it should work.


    jpwrunyan Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139