Graphics

 View Only
  • 1.  Change Text Color

    Posted 05-15-2013 19:42
    I have a text box getting a number from a datalinq feed. If the number coming in is 0 or higher, I want the number to be green; if negative, I would like the number to be red. How do I script this?


  • 2.  RE: Change Text Color

    Posted 05-15-2013 20:41
    I use font names rather than their IDs, though you can use either...

    On your text object, try this script:

    dim number as xpTextObject

    scene.getObjectByName("Text1", number)

    if cInt(text) >= "0" then

    number.textwithtags = "{font_green}" & number.textwithtags

    else

    number.textwithtags = "{font_red}" & number.textwithtags

    end if


    #XPression


  • 3.  RE: Change Text Color

    Posted 05-17-2013 14:28
    not sure what I'm doing wrong, but this script is not working for me. I even renamed all of my fonts and the text object to match the language in this script. It keeps the original font the scene is created with no matter what the value of the number is.

    Any other ideas?

    #XPression


  • 4.  RE: Change Text Color

    Posted 05-17-2013 15:24
    here's a link to hopefully download what I did so you can take a look at it.

    http://www.filedropper.com/fontcolortest

    #XPression


  • 5.  RE: Change Text Color

    Posted 05-17-2013 17:10
    Hi Brian,

    Just change your script to this and it's working correctly in your sample project:



    if cInt(text) >= "0" then

    text = "{green}" & text

    else

    text = "{red}" & text

    end if



    The GetObjectByName and textwithtags calls in the original sample are not actually needed. Also, your fonts were called "red" and "green" so the script should use "red" not "font_red".

    #XPression


  • 6.  RE: Change Text Color

    Posted 05-17-2013 17:38
    Perfect! Thanks a bunch!

    #XPression


  • 7.  RE: Change Text Color

    Posted 05-17-2013 17:39
    On a related subject, is there a way to get the font to hide the minus symbol but still change the text color based on the value?

    #XPression


  • 8.  RE: Change Text Color

    Posted 05-17-2013 18:03
    Or how would I script this text box to change its color based upon the positive or negative value of the text in a second box?

    #XPression


  • 9.  RE: Change Text Color

    Posted 05-17-2013 20:29
    To remove the minus sign you could do this:



    if cDbl(text) >= "0" then

    text = "{green}" & text

    else

    text = "{red}" & mid(text,2)

    end if



    mid(text,2) will strip the first character off of the negative value (which should be the minus sign).

    Note that I also change the first line from cInt to cDbl so that this function works better with negative decimal values.

    #XPression