Lazarus
Programming => Graphics and Multimedia => Graphics => Topic started by: cov on June 07, 2013, 11:21:18 am
-
Hi,
I have a string 'A long sentence which is split'+#13+#10+'over multiple lines'.
I'm using Canvas.Textout to write it to the Canvas.
Is there a way of doing this neatly?
Or must I split it into a TStringList and TextOut each entry?
-
Canvas has TextOut and TextRect methods which does the same - write text to canvas, but TextRect seems to be more adjustible.
Instead of Canvas.TextOut, use Canvas.TextRect, but first adjust Canvas.TextStyle as in the following example:
procedure TForm1.FormPaint(Sender: TObject);
var
TextStyle: TTextStyle;
begin
TextStyle := Canvas.TextStyle;
TextStyle.SingleLine := False;
TextStyle.Wordbreak := True;
TextStyle.Opaque := False;
Canvas.TextStyle := TextStyle;
Canvas.TextOut(5, 5, 'trt' + LineEnding + 'mrt'); // line breaking does not work
Canvas.TextRect(Self.ClientRect, 45, 45, 'trt' + LineEnding + 'mrt'); // line breaking works!
end;
-
Thank you very much Zoran.
-
Yes, indeed, thanks, Zoran.
A question, though: TextRect requires 4 arguments: a TRect, an X position, a Y position and the string.
How do you define the TRect?
if TextExtent:=Image1.Canvas.TextExtent('A long sentence which is split'+#13+#10+'over multiple lines');
I would have thought that giving Rect(0,0,TextExtent.cx,TextExtent.cy) would have been okay.
But it seems that the following is needed:
Image1.Canvas.TextRect(Rect(x,y,TextExtent.cx+x,TextExtent.cy+y),x,y,'A long sentence which is split'+#13+#10+'over multiple lines');
Is this correct?
-
Thank you very much Zoran.
Yes, indeed, thanks, Zoran.
You are welcome :)
A question, though: TextRect requires 4 arguments: a TRect, an X position, a Y position and the string.
How do you define the TRect?
if TextExtent:=Image1.Canvas.TextExtent('A long sentence which is split'+#13+#10+'over multiple lines');
I would have thought that giving Rect(0,0,TextExtent.cx,TextExtent.cy) would have been okay.
But it seems that the following is needed:
Image1.Canvas.TextRect(Rect(x,y,TextExtent.cx+x,TextExtent.cy+y),x,y,'A long sentence which is split'+#13+#10+'over multiple lines');
Is this correct?
It seems to be correct. I know nothing more about it than what I learned by trying. Try for yourself.
When TextStyle.Opaque is set to True, you can see the rectangle.
I suggest you to play with TextStyle values and see what you get. ;)
See here about TextStyle values: http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/ttextstyle.html
Something about TextRect method: http://lazarus-ccr.sourceforge.net/docs/lcl/graphics/tcanvas.textrect.html. You can see there that you can also use TextStyle directly as a TextRect parameter, in longer version of this method (instead of setting the Canvas.TextStyle property).