[英]in indesign javascript. how to select some text in textframe and color this
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);}
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
). 第一次尝试是在开始处插入文本,然后为整个段落设置颜色(您的
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. 这是因为
InsertionPoint
行为类似于文本光标 :就像在界面本身中一样,您可以“设置”属性(例如颜色,字体或文本大小),然后立即在相同位置“键入”某些文本。
You can do this on any InsertionPoint, not only at the start of a paragraph. 您不仅可以在段落开头,还可以在任何InsertionPoint上执行此操作。 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
; InDesign足够聪明,可以在索引
words
和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. 您还将看到它们之间的空格也带有下划线,因为它们是您所指代字符范围的一部分。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.