【问题标题】:one line with multiple color text in phpword?phpword 中有多种颜色文本的一行?
【发布时间】:2019-07-19 11:45:26
【问题描述】:

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

我希望我的线路是这样的:

这是我的代码:

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

【问题讨论】:

    标签: php phpword


    【解决方案1】:

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

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

    示例代码:

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

    # 不用于颜色。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-08-27
        • 2014-01-28
        • 1970-01-01
        • 2012-07-16
        • 2016-10-10
        • 1970-01-01
        • 2021-09-08
        • 2012-02-15
        相关资源
        最近更新 更多