PHPWord 单元格中文本的垂直对齐方式

问题描述 投票:0回答:3

我正在尝试在 PHPWord 的单元格中垂直对齐文本。

$cellHCentered = array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER);

适用于水平对齐,但我找不到垂直对齐的解决方案,有什么想法吗?

phpword
3个回答
3
投票

解决方案可能并不完美!

检查以下代码第三行的 spaceAfter 和 spaceBefore - 在文本前后添加空格。我分配了250。你可以提供你自己的值。

$table = $section->addTable(['borderSize' => 6]);
$table->addRow(); 
$table->addCell(2000)->addText("Text in the Cell",['name'=>'Times New Roman','size' => 12],['spaceAfter' => 250,'spaceBefore' => 250]);

0
投票

这有效:

        $cell = $table->addCell(\PhpOffice\PhpWord\Shared\Converter::inchToTwip(1.25), [
            'valign' => \PhpOffice\PhpWord\SimpleType\VerticalJc::BOTTOM
        ]);
        $cell->addText('Text', null, [
            'spaceBefore' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(0),
            'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::inchToTwip(0),
            'align' => 'end'
        ]);

尽管如此,手册实际上说要使用这个:

'valign' => 'bottom'

顺便说一句,这些功能还有:

'vAlign' => \PhpOffice\PhpWord\SimpleType\VerticalJc::BOTTOM

'vAlign' => 'bottom'

所以我得出结论,部分和单元格垂直对齐之间存在一些重叠。


0
投票

创建 Cell 时,传递 ['valign' => 'center'] 作为样式参数:

$table->addCell($someWidth, ['valign' => 'center'])->addText(...);
© www.soinside.com 2019 - 2024. All rights reserved.