Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically created THTMLabel.Height always return default value?

Tags:

label

delphi

I am creating a number of dynamically created THTMLabels but after these are created,when I try to get it's height,it always return the default height value.

Here is my code:

for i := 0 to ASentencePtr^.MUS.Count - 1 do
begin
  j := Random(slTemp.Count);
  sSen := ASentencePtr^.MUS.Strings[StrToInt(slTemp.Strings[j])] + ' / ';

  THTMLabel.Create(Self).Name := 'lblSen_' + slTemp.Strings[j];
  with THTMLabel(FindComponent('lblSen_' + slTemp.Strings[j])) do
  begin
    Font.Size := 18;
    Font.Style := [fsBold];
    Parent := FlowPanel1;
    Width := Parent.Width;
    Cursor := crHandPoint;
    DragMode := dmAutomatic;
    ControlStyle := ControlStyle + [csDisplayDragImage];
    HTMLText.Add(sSen);
    Autosizing := True;        
  end;

  slTemp.Delete(j);
end;

Now when I try to access THTMLabel(FindComponent('lblSen_0')).Height, it returns only the default value which is 17. Where have I gone wrong? Any thoughts anyone? Any help is greatly appreciated, thanks.

like image 763
jhodzzz Avatar asked Jun 15 '26 10:06

jhodzzz


1 Answers

We had the same problems but managed to solve them with the THTMLStaticText component and this function that calculates the height when dynamically (height) adjusted:

function CalculateDynamicHeight( aLabel: TLabel; htmlStaticText: THTMLStaticText): Integer;
var
  lRect : TRect;
  lText : string;
begin
  lRect := Rect( 0, 0, htmlStaticText.Width, 0);
  lText := htmlStaticText.Text;

  aLabel.Caption := htmlStaticText.Text;
  aLabel.Font := htmlStaticText.Font;
  aLabel.Canvas.Font := htmlStaticText.Font;
  aLabel.Canvas.TextRect(
            {var} lRect, //will be modified to fit the text dimensions
            {var} lText, //not modified, unless you use the "tfModifyingString" flag
            [tfCalcRect, tfWordBreak] //flags to say "compute text dimensions with line breaks"
          );
  ASSERT( lRect.Top = 0 ); //this shouldn't have moved
  aLabel.Height := lRect.Bottom;

  Result := lRect.Bottom;
end;

The function requires a TLabel component, used exclusively for calculation purposes (you can set the visibility to false). The htmlStaticText component should have AutoSize set to true (in our case AutoSizeType is set to asVertical) and the htmlStaticText.Text should be present when calling the function.

like image 137
Pinta Avatar answered Jun 17 '26 23:06

Pinta