* * *

Author Topic: Newline in canvas.Textout  (Read 4804 times)

cov

  • Full Member
  • ***
  • Posts: 222
Newline in canvas.Textout
« 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?

Zoran

  • Hero Member
  • *****
  • Posts: 1220
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Newline in canvas.Textout
« Reply #1 on: June 07, 2013, 11:42:26 am »
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

  • Hero Member
  • *****
  • Posts: 668
    • exdatis
Re: Newline in canvas.Textout
« Reply #2 on: June 07, 2013, 12:10:43 pm »
Thank you very much Zoran.

cov

  • Full Member
  • ***
  • Posts: 222
Re: Newline in canvas.Textout
« Reply #3 on: June 07, 2013, 12:41:56 pm »
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

  • Hero Member
  • *****
  • Posts: 1220
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Newline in canvas.Textout
« Reply #4 on: June 07, 2013, 02:09:27 pm »
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).


 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus