在indesign javascript中。如何在textframe中选择一些文本并为其着色

时间:2022-09-09 12:37:21

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);}  

1 个解决方案

#1


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.

在第一次尝试时,您将在开头插入文本,然后设置整个段落的颜色(textHeaderTf)。在第二次尝试中,您只设置第一个单词的颜色。

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.

这是因为InsertionPoint的行为类似于文本光标:就像在界面本身一样,您可以“设置”一个属性,如颜色,字体或文本大小,然后立即在同一位置“键入”某些文本。

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.

您可以在任何InsertionPoint上执行此操作,而不仅仅是在段落的开头。例如,它可以在第三个单词之前添加文本。

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.

InDesign足够聪明,可以在索引单词和字符之间进行转换;你会看到中间的空格也加下划线,因为它们是你所引用的字符范围的一部分。

#1


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.

在第一次尝试时,您将在开头插入文本,然后设置整个段落的颜色(textHeaderTf)。在第二次尝试中,您只设置第一个单词的颜色。

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.

这是因为InsertionPoint的行为类似于文本光标:就像在界面本身一样,您可以“设置”一个属性,如颜色,字体或文本大小,然后立即在同一位置“键入”某些文本。

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.

您可以在任何InsertionPoint上执行此操作,而不仅仅是在段落的开头。例如,它可以在第三个单词之前添加文本。

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.

InDesign足够聪明,可以在索引单词和字符之间进行转换;你会看到中间的空格也加下划线,因为它们是你所引用的字符范围的一部分。