2016-08-01 64 views
0

。 但我只需要颜色的第一个单词,并在第二次,我不能仅颜色选定的单词。请帮帮我!谢谢。in indesign javascript。如何选择文本框中的一些文本,并在第一次为文本框和颜色文本和背景添加颜色

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

回答

0

这取决于你想要以什么样的顺序工作。

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

如果你想在一开始有它的彩色文本添加马上,使用此:

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

这是因为InsertionPoint表现得像文本光标:就像在接口本身,您可以'设置'一个属性,如颜色,字体或文本大小,然后立即在同一位置键入一些文本。

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

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

如果你要的颜色了一些现有的话,你可以用一个循环计数不送:

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

记住你还是处理个人,虽然。若您重复使用

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

你会看到,是的,词强调的 - 但也许你想强调它们之间的空间为好。

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

InDesign中是足够聪明的索引words之间进行转换:

要做到这一点,你可以通过解决第一个和最后一个字之间的字符范围针对整个文本块一气呵成和characters;您会看到两者之间的空格也会被加下划线,因为它们是您引用的字符范围内的一部分。