我正在使用以下代码通过FPDF将标题和日期彼此相邻显示:
$this->SetFont('Helvetica', 'B', 30);
$this->Cell(120, 20, 'Rechnung 20130809-78');
$this->SetFont('Helvetica', '', 10);
$this->Cell(0, 20, '09. 08. 2013');
但是文本对齐不正确:
如何使它工作,以使基线处于相同高度?
我不想要一种必须手动调整其中一个元素的位置的解决方案。它必须适用于我输入的每种字体大小。
我已经尝试过在Cell方法中自动调整y位置,但是文本然后不在基线处对齐,而是在底部(g终止处)对齐!
public function Cell($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') {
$text = utf8_decode($txt);
$startX = $this->GetX();
$startY = $this->GetY();
$this->SetY($startY - $this->FontSize / 2);
$this->SetX($startX);
parent::Cell($w, $h, $txt, $border, $ln, $align, $fill, $link);
$endX = $this->GetX();
$endY = $this->GetY();
$this->SetY($startY);
$this->SetX($endX);
}
有什么办法可以做我打算做的事吗?请帮我!上图中的绿线应处于相同的高度。
最佳答案
这是一个解决方案:
function drawTextBox($strText, $w, $h, $align='L', $valign='T', $border=true)
{
$xi=$this->GetX();
$yi=$this->GetY();
$hrow=$this->FontSize;
$textrows=$this->drawRows($w,$hrow,$strText,0,$align,0,0,0);
$maxrows=floor($h/$this->FontSize);
$rows=min($textrows,$maxrows);
$dy=0;
if (strtoupper($valign)=='M')
$dy=($h-$rows*$this->FontSize)/2;
if (strtoupper($valign)=='B')
$dy=$h-$rows*$this->FontSize;
$this->SetY($yi+$dy);
$this->SetX($xi);
$this->drawRows($w,$hrow,$strText,0,$align,false,$rows,1);
if ($border)
$this->Rect($xi,$yi,$w,$h);
}
来源:https://github.com/lsolesen/fpdf/blob/master/examples/textbox/textbox.php
还有一个附加组件:http://fpdf.de/downloads/addons/52/