0

in first times i took text frame and color text and background. but i need color only first words, and in second times i can not color only selected word. please, help me! thanks.

var textHeaderTf;
                try{
                 textHeaderTf = headerTf.paragraphs.item(0);
                 if(textHeaderTf!=undefined && textHeaderTf!=null)
                 {
                    headerTf.parentStory.insertionPoints.item(-1).contents = 'myNewText';
                   // textHeaderTf.fillColor  = myColorA; 
                    textHeaderTf.strokeColor  = myColorB; 

                 }
                }catch(e){log.write('setHeader font-color error7'+e);}  

                try{
                 textHeaderTfWord = textHeaderTf.words[0];//headerTf.paragraphs.item(1);
                 if(textHeaderTfWord!=undefined && textHeaderTfWord!=null)
                 {
                    textHeaderTfWord.fillColor  = myColorA;
                    textHeaderTfWord.strokeColor  = myColorA; 

                 }
                }catch(e){log.write('setHeader font-color error8'+e);}  
Serjo
  • 1
  • 1
  • 1

1 Answers1

0

It depends on in what order you want to work.

In your first attempt, you are inserting text at the start and then you set the color for an entire paragraph (your textHeaderTf).
In your second attempt, you set the color of the first word only.

If you want to add text at the start and have it colored right away, use this:

textHeaderTf.insertionPoints.item(0).fillColor = myColorA;
textHeaderTf.insertionPoints.item(0).contents = "Colored text!";

That is because an InsertionPoint behaves like a text cursor: just like in the interface itself, you can 'set' an attribute such as color, font, or text size, and then immediately 'type' some text in the same position.

You can do this on any InsertionPoint, not only at the start of a paragraph. It works the same for adding text before the 3rd word, for example.

textHeaderTf.words.item(2).insertionPoints.item(0).fillColor = myColorA;
textHeaderTf.words.item(2).insertionPoints.item(0).contents = "more colored text here ";

If you want to color a number of existing words, you can count them off with a loop:

for (i=0; i<5; i++)
    textHeaderTf.words.item(i).fillColor = myColorA;

Keep in mind you are still addressing individual words, though. If you repeat this with

for (i=0; i<5; i++)
    textHeaderTf.words.item(i).underline = true;

you will see that, yes, the words are underlined – but perhaps you wanted to underline the spaces between them as well.

To do so, you can target an entire chunk of text in one go by addressing the character range between the first and the last words:

textHeaderTf.characters.itemByRange(textHeaderTf.words.item(1),
    textHeaderTf.words.item(4)).underline = true;

InDesign is smart enough to translate between indexing words and characters; you will see the spaces in between get underlined as well, since they are part of the range of characters that you refer to.

Jongware
  • 22,200
  • 8
  • 54
  • 100