简体   繁体   English

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

[英]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足够聪明,可以在索引wordscharacters之间进行翻译; 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.

相关问题 如何在Indesign中更改文本框架创建顺序 - How to change textframe creation order in Indesign 如何使用javascript将各种线程化文本框架的所有内容复制到indesign中的另一个文本框架? - How to copy all the contents of various threaded textframes to another textframe in indesign with javascript? InDesign使用Java更改文本笔触颜色 - InDesign changing Text Stroke Color with Javascript 用于添加多边形(不规则形状文本框)并向其添加文本的 indesign 脚本 - indesign script to add a polygon (irregular shape textframe) and add text to it 如何通过 JavaScript 在 InDesign 中查找所有文本框 - How to find ALL text frames in InDesign by JavaScript 在设计中获取选定文本框的数量 - grab the count of selected textframe indesign JavaScript。 如何通过数组函数更改对象中的某些数据? - JavaScript. How to change some data in object via function for array? 在页面中手动更改后,如何在InDesign中设置(textFrame)pageItem以从母版页更新位置/大小? - How to set a (textFrame) pageItem in InDesign to update the position/size from the master page after it was manually changed in a page? 选择InDesign中线程中的所有文本? - Select all text in thread in InDesign? 如何修改从InCopy创建的InDesign文档textFrame? - How can I modify an InDesign's document textFrame created from InCopy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM