Consider the following code:
procedure TForm1.PaintBox1Paint(Sender: TObject);
var
vRect : TRect;
vFormat : TTextFormat;
vStr : string;
begin
vStr := 'This is some text';
vRect := rect(10,10,50,130);
vFormat := [tfCenter,tfVerticalCenter,tfWordBreak];
PaintBox1.Canvas.Rectangle(vRect);
PaintBox1.Canvas.TextRect(vRect,vStr,vFormat);
end;
I would expect something like this
+--------+
| |
| |
|This is |
| some |
| text |
| |
| |
+--------+
but I get this
+--------+
|This is |
| some |
| text |
| |
| |
| |
| |
+--------+
The same is true for the tfBottom format. The horizontal text formats (tfLeft, tfRight, tfCenter) work as expected, but the vertical formats doesn't. Can anyone explain this?
I'm not pretty sure but VerticalCenter is only allowed if singleline is set. This is because Canvas relies on native windows functions. If you look at Windows DrawText function you will see this restriction. If you need to vertical center you have to do your own maths
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With