1

有没有什么方法可以像这样在同一行写不同颜色的文本但它会改变整行的颜色?

我希望我的台词喜欢这样:
在此处输入图像描述

这是我的代码:

$section->addText( 'Headline: ', (array('color'=>'#70AD47')),$fontStyleIndexPara);
            $section->addText(cleanstring($data[$k]['ArticleTitle']),$fontStyleIndexPara);
           `
4

2 回答 2

3

不要使用 addText() 将单个文本部分直接写入该部分,而是使用 addTextRun() 将其括起来。

通过使用 addTextRun() 您可以防止换行。

您的示例代码:

$TextRun = $section->addTextRun();
$TextRun->addText( 'Headline: ', array('color'=>'70AD47'));
$TextRun->addText(cleanstring($data[$k]['ArticleTitle']));

# 不用于颜色。

于 2019-11-07T11:58:42.630 回答
0

是的,您可以使用类似的东西:

$sentence='Your text in this sentence the two first word will get the stylefont while the rest will get the stylefont2';
$word_arr=explode(' ', $sentence);

$phpWord->addParagraphStyle('p3Style',array('align'=>'center'));
$c = 0;
$textrun = $section->addTextRun(array('align' => 'center'));
for($i = 0; $i < count($word_arr); $i++)
{
    $c++;
    if($c < 2)
    {
        $textrun->addText($word_arr[$i].' ', $styleFont);
    }
    else
    {
        $textrun->addText($word_arr[$i].' ', $styleFont2);
    }
}
于 2019-07-19T12:04:03.120 回答