jhodzzz
jhodzzz

Reputation: 83

Dynamically created THTMLabel.Height always return default value?

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.

Upvotes: 3

Views: 478

Answers (2)

Pinta
Pinta

Reputation: 11

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.

Upvotes: 1

Christopher Ramírez
Christopher Ramírez

Reputation: 1720

I think THTMLLabel is part of the Jedi Library, itsn't? This problem is weird. I don't know the implementation of this control, but if you are having this problem, then there's may be a very bad implementation of the AutoSize Property.

If you can't inspect and fix the source of this control, try to use the BoundsRect property to get the height:

LabelHeight := THTMLabel(FindComponent('lblSen_0')).BoundsRect.Bottom;

Upvotes: 0

Related Questions