Board index » delphi » getting the width of a string

getting the width of a string

Hi all,
Does anyone know how to get the width of a string in a certain font?

Any help would be appreciated.

Thanks,
Derek

 

Re:getting the width of a string


Derek,
  Take a look at the Textwidth function...
                                         Dave
                                          http://www.erols.com/dparsons

Quote
Derek Cheng wrote in message <3501f7d...@news.cadvision.com>...
>Hi all,
>Does anyone know how to get the width of a string in a certain font?

>Any help would be appreciated.

>Thanks,
>Derek

Re:getting the width of a string


Quote
Derek Cheng wrote:

> Hi all,
> Does anyone know how to get the width of a string in a certain font?

> Any help would be appreciated.

> Thanks,
> Derek

Hi Derek

I create a dummy label and sets its AutoSize to true, like this:

function ReturnStringWidth(MyFont : TFont; SomeString : string) :
integer;
var  TL : TLabel;
begin
   TL := TLabel.Create(Form1);
   TL.Parent := Form1;
   TL.AutoSize := True;
   TL.Font := MyFont;

   // Apply the string to the labels caption
   TL.Caption := SomeString;

   // Now, simply query the width of the Label since its AutoSize
property is set to true.
   ReturnStringWidth := TL.Width;
   TL.Free;
end;    

Hope this helps

Best regards,
Karsten Heitmann

-------------------------------------------------------------------
Please remove country names in my email address before replying.
This is done to avoid spam.
-------------------------------------------------------------------

Re:getting the width of a string


Nice solution, but I would like to say, set TL.visible:=false, this to
prevent any possible 'flashes' on Form1. best do this before Parent is set.

HTH

  Roel

PS. how's the weather in Denmark ?

Quote
Karsten Heitmann <he...@CANADAJAPANSWEDENpost1.tele.dk> wrote:

: Hi Derek

: I create a dummy label and sets its AutoSize to true, like this:

: function ReturnStringWidth(MyFont : TFont; SomeString : string) :
: integer;
: var  TL : TLabel;
: begin
:    TL := TLabel.Create(Form1);
:    TL.Parent := Form1;
:    TL.AutoSize := True;
:    TL.Font := MyFont;

:    // Apply the string to the labels caption
:    TL.Caption := SomeString;

:    // Now, simply query the width of the Label since its AutoSize
: property is set to true.
:    ReturnStringWidth := TL.Width;
:    TL.Free;
: end;    

: Hope this helps

: Best regards,
: Karsten Heitmann

Re:getting the width of a string


Gee, why make it so tuff?  Why not use the TextWidth method of the Canvas?

***The following was pasted from the Delphi Help File***

function TextWidth(const Text: string): Integer;

Description
 The TextWidth method returns the width in pixels of the string passed in
Text when rendered in the current font. You can use TextWidth to determine
whether a given string will fit in a particular space.

John

Re:getting the width of a string


Quote
>   Roel Spapens <ro...@stack.nl> writes:
>  Nice solution, but I would like to say, set TL.visible:=false, this to
>  prevent any possible 'flashes' on Form1. best do this before Parent is set.

There is also (straight from the helpfile:)

TextWidth returns the width, in pixels, of a string rendered in the current font.

function TextWidth(const Text: string): Integer;

Description

Use TextWdith to determine the length a string will occupy in the image. TextWidth indicates whether a given string will fit in the available space. Other graphical
elements in the image such as lines, or additional strings can be positioned to accommodate the width of the text.
TextWidth returns the same value as TextExtent(Text).cx.

johan

Other Threads