RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCanvas.TextHeight Method

Returns the height, in pixels, of a string rendered in the current font.

Pascal
function TextHeight(const Text: string): Integer;
C++
__fastcall int TextHeight(const AnsiString Text);

Use TextHeight to determine the height a string will occupy in the image. Other elements in the image such as lines, boxes, or additional lines of text can be positioned to accommodate the height of the text. TextHeight returns the height of the font, the ascenders plus the descenders, not the actual height of the specified string. If Font.Height is negative, then TextHeight is equal to abs(Font.Height) plus the internal leading of the font. If Font.Height is positive, then TextHeight equals Font.Height. 

TextHeight returns the same value as TextExtent(Text).cy.  

C++ Examples: 

 

/*
This project requires a TPageControl and several TTabPages.
To add new pages, right-click the TPageControl and select 
New Page.  During the form creation the width and height of 
all tabs on the Page Control becomes double the largest 
previous value. So if there are three tabs with captions that
are 12, 23, and 19 characters long, the TabWidth would be the
number of pixels required to fit 46 characters.
*/
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  for (int i = 0; i < PageControl1->PageCount; i++)
  {
     AnsiString s = PageControl1->Pages[i]->Caption;
     if (Canvas->TextWidth(s) * 2 > PageControl1->TabWidth)
        PageControl1->TabWidth = Canvas->TextWidth(s) * 2;
      if (Canvas->TextHeight(s) * 2 > PageControl1->TabHeight)
        PageControl1->TabHeight = Canvas->TextHeight(s) * 2;
  }
}

 

Delphi Examples: 

{
This project requires a TPageControl and several TTabPages.
To add new pages, right-click the TPageControl and select 
New Page.  During the form creation the width and height of 
all tabs on the Page Control becomes double the largest 
previous value. So if there are three tabs with captions that
are 12, 23, and 19 characters long, the TabWidth would be the
number of pixels required to fit 46 characters.
} 
procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  with PageControl1 do
  begin
    for i := 0 to PageCount - 1 do
    begin
      if (Canvas.TextWidth(Pages[i].Caption) * 2) > TabWidth then
        TabWidth := Canvas.TextWidth(Pages[i].Caption) * 2;
      if (Canvas.TextHeight(Pages[i].Caption) * 2) > TabHeight then
        TabHeight := Canvas.TextHeight(Pages[i].Caption) * 2;
    end;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!