Recent

Author Topic: Draw autosize text in rect  (Read 3214 times)

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Draw autosize text in rect
« on: November 17, 2015, 09:31:10 am »
Hi,

How can I draw text in rect with an Autosize system that it choose what font size it need to be fit in rect.
It can be good to be in BGRABitmap or default canvas or whatever.

balazsszekely

  • Guest
Re: Draw autosize text in rect
« Reply #1 on: November 17, 2015, 09:45:00 am »
Let's say you want to fit a specific text into a panel. You can do something like this:
Code: Pascal  [Select][+][-]
  1. var
  2.   Txt: string;
  3.   w, h: Integer;  
  4. begin
  5.   Txt := 'This is a test';
  6.   Panel1.Canvas.Font.Name := 'Tahoma';
  7.   Panel1.Canvas.GetTextSize(Txt, w, h);
  8.   while (w < Panel1.Canvas.Width) and (h < Panel1.Canvas.Height) do
  9.   begin
  10.     Panel1.Canvas.Font.Size := Panel1.Canvas.Font.Size + 1;
  11.     Panel1.Canvas.GetTextSize(Txt, w, h);
  12.   end;
  13.   ShowMessage(IntToStr(Panel1.Canvas.Font.Size));
  14. end;

In the end you will get the biggest font of the text which still fits into the panel.  Is this what are you after?

aradeonas

  • Hero Member
  • *****
  • Posts: 824
Re: Draw autosize text in rect
« Reply #2 on: November 17, 2015, 12:11:36 pm »
Thanks.
I add a demo in BGRABitmap board here.

 

TinyPortal © 2005-2018