Jump to content

Changing Text Color Based on Field Value


rjohns

Recommended Posts

I'd like to change the text color for all of the text in a specific text frame based on the value of a field. For example, if the field value contains the word "color," change the text color for all the text in the text frame named "Address" to Pantone 424 C.

 

I've tried searching the existing posts, but I haven't found one addressing this specific topic.

 

The best I've come up with so far is:

if (Field("SizeColor").indexOf("color") > -1)
{
  return FindTextFrame("Address").content("<color name=\"PANTONE 424 C\">")
}

return "";

 

Any help would be much appreciated – thanks!

Link to comment
Share on other sites

In OnRecordStart, you should be able to do this:

if (Field("SizeColor").indexOf("color") > -1)
   FindTextFrame("Address").content = '<span color="PANTONE 424 C">' + FindTextFrame("Address").content;

Or, you can create a regular Text rule like so:

if (Field("SizeColor").indexOf("color") > -1)
   return '<span color="PANTONE 424 C">';
//else
return "";

And, with the Text Editor dialog, insert the name of that rule as a variable at the start of the text for the frame in question.

Link to comment
Share on other sites

I'm not quite sure what you're trying to achieve here, but if you want to change all of the text in the frame "Address" to "PANTONE 424 C" when the field "SizeColor" contains the word "color" somewhere in it, you can do this:

OnRecordStart

var field = Field("SizeColor");
var color = "PANTONE 424 C";
if (field.match(/color/)) {
   FindTextFrame("Address").content = '<span color="' + color +'">' + FindTextFrame("Address").content + '</span>';
}

 

The above is essentially wrapping the entire contents of the frame with span tags. I should note: any text in that frame that previously had span tags applied to it will remain the color of their previous span tags by nature of how span tags work. Meaning, if you have JavaScript rules that are returning (span) tagged text in a color other than Pantone 424 (for example: blue), that text will remain blue.

 

If that's the case you'd want to do a find and replace on all of your color tags in the "address" text frame and update them to Pantone 424. But that's a little more convoluted:

var field = Field("SizeColor");
var color = "PANTONE 424 C";

if (field.match(/color/)) {
  var text = FindTextFrame("Address").content;
  text = text.replace(/<variable name="([^"]*)">/g, function(s,p){return FieldOrRule(p);});
  text = text.replace(/(<color name=")[^"]*(">)|(<span(??!color).)*color=")[^"]*(">)/g, '$1' + color + '$2' );
  FindTextFrame("Address").content = text;
} 

Link to comment
Share on other sites

  • 5 months later...

This is exactly what I'm attempting to do. I'm using the text rule and have inserted it into the text editor dialog box but it is returning the literal text of the rule. I'm missing one small step I believe here. I've created four new colors named, Spot Red, Spot Green, Spot Blue, and Spot gray. Here's what I've got so far:

 

 

 

if (Field("Background").indexOf("BLUE") > -1)

return '<span color="Spot Blue">';

if (Field("Background").indexOf("GREEN") > -1)

return '<span color="Spot Green">';

if (Field("Background").indexOf("RED") > -1)

return '<span color="Spot Red">';

if (Field("Background").indexOf("GRAY") > -1)

return '<span color="Spot Gray">';

//else

return "";

 

 

Its functioning but its returning the literal text in the text box - for example:

 

<span color="Spot Gray">

 

or

 

<span color="Spot Red">

 

What am I missing here guys? Thanks in advance.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...