繁体   English   中英

在FPDF中按基线对齐文本

[英]Align text by baseline in FPDF

我正在使用以下代码通过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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM