Newline in canvas.Textout lazarus / 39-Graphics ---------------------------------------------------------------------- cov · 2013-06-07 09:21 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? ---------------------------------------------------------------------- Zoran · 2013-06-07 09:42 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: Code: [Select]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; ---------------------------------------------------------------------- exdatis · 2013-06-07 10:10 Thank you very much Zoran. ---------------------------------------------------------------------- cov · 2013-06-07 10:41 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? ---------------------------------------------------------------------- Zoran · 2013-06-07 12:09 Quote from: exdatis on June 07, 2013, 12:10:43 pmThank you very much Zoran. Quote from: cov on June 07, 2013, 12:41:56 pmYes, indeed, thanks, Zoran. You are welcome Quote from: cov on June 07, 2013, 12:41:56 pmA 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).