TLabel - did text fit? lazarus / 21-General ---------------------------------------------------------------------- domasz · 2023-06-26 12:51 I have a TLabel with AutoSize := False, Width = 200, Height = 100. I assign text to the Caption. How can I tell if the text fit or if it was cut? ---------------------------------------------------------------------- Zvoni · 2023-06-26 13:16 Quote from: domasz on June 26, 2023, 02:51:40 pmI have a TLabel with AutoSize := False, Width = 200, Height = 100. I assign text to the Caption. How can I tell if the text fit or if it was cut? Add a hidden TLabel with Autosize:=True, assign the Caption to both Labels, compare Height/Width of both. If hidden Height/Width greater then visible Label...... ---------------------------------------------------------------------- KodeZwerg · 2023-06-26 13:25 or do it by code, like here Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit Unit1; {$mode objfpc}{$H+} interface uses  Classes , SysUtils , Forms , Controls , Graphics , Dialogs , StdCtrls ,  ExtCtrls; type   { TForm1 }   TForm1 = class(TForm)    Button1: TButton;    Label1: TLabel;    LabeledEdit1: TLabeledEdit;    procedure Button1Click(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } function GetFontSize(const AText: String; const AFont: TFont; out AWidth, AHeight: Integer): Boolean;var  LBMP: TBitmap;begin  Result := False;  AWidth := 0;  AHeight := 0;  LBMP := TBitmap.Create;  try    LBMP.Canvas.Font.Assign(AFont);    AWidth := LBMP.Canvas.TextWidth(AText);    AHeight := LBMP.Canvas.TextHeight(AText);    Result := ((AWidth <> 0) or (AHeight <> 0));  finally    LBMP.Free;  end;end; procedure TForm1.Button1Click(Sender: TObject);var  s: string;  LWidth,  LHeight: Integer;begin  s := LabeledEdit1.Text;  if GetFontSize(s, Label1.Font, LWidth, LHeight) then    begin      if Label1.Width >= LWidth then        Label1.Caption := s      else        Label1.Caption := 'Text is too big!';    end  else    Label1.Caption := 'Error calling GetFontSize!';end; end. ---------------------------------------------------------------------- paweld · 2023-06-26 13:38 @Zvoni - will not work well if the label has the WordWrap property set to True.Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  LCLIntf, LCLType; procedure TForm1.Button1Click(Sender: TObject);var  rect: TRect;  flags: Cardinal;  ltmp: TLabel;begin  if Label1.AutoSize then  begin    ShowMessage('Label size is ok');    exit;  end;  ltmp := TLabel.Create(Self);  ltmp.Parent := Self;  ltmp.AutoSize := False;  ltmp.Font := Label1.Font;  ltmp.WordWrap := Label1.WordWrap;  ltmp.Width := Label1.Width;  ltmp.Height := Label1.Height;  ltmp.Caption := Label1.Caption;  rect := ltmp.BoundsRect;  ltmp.Visible := False;  flags := DT_CALCRECT;  if ltmp.WordWrap then  flags := flags or DT_WORDBREAK;  DrawText(ltmp.Canvas.Handle, PChar(ltmp.Caption), Length(ltmp.Caption), rect, flags);  if (Label1.Height >= rect.Height) and (Label1.Width >= rect.Width) then  ShowMessage('Label size is ok')  else    ShowMessage(Format('Label is too small %d x %d > %d x %d', [Label1.Width, Label1.Height, rect.Width, rect.Height]));  ltmp.Free;end; ---------------------------------------------------------------------- Zvoni · 2023-06-26 14:27 Quote from: paweld on June 26, 2023, 03:38:02 pm@Zvoni - will not work well if the label has the WordWrap property set to True Agreed. OTOH, the OP wants to find out, if his Caption gets cut off, so WordWrap wouldn't make sense for that. and i implied, that the hidden Label has everything the same as the visible except AutoSize ---------------------------------------------------------------------- Warfley · 2023-06-26 14:42 Quote from: Zvoni on June 26, 2023, 03:16:07 pmAdd a hidden TLabel with Autosize:=True, assign the Caption to both Labels, compare Height/Width of both. If hidden Height/Width greater then visible Label...... From my experience this does not always work quite as well as expected. Sometimes the size will not be correctly computed until a few messages have been sent by Windows, so you either have to have a few Application.ProcessMessages, but the best way is usually to just use Canvas.TextHeight and Canvas.TextWidth. Quote from: Zvoni on June 26, 2023, 04:27:25 pmOTOH, the OP wants to find out, if his Caption gets cut off, so WordWrap wouldn't make sense for that. and i implied, that the hidden Label has everything the same as the visible except AutoSize It does, the Last time I have used this, I basically had the following situation, I had 3 labels, a header, a footer and a mid part. All of those had a fixed width, with wordwrap on, so I was only interested basically in how many lines the wordwrap would split it and how much height these lines would take up. This way I could ensure to properly cut off parts, if the size of the three labels would exceed the size of the container they were on. Just as an example on why it may be of interest to compute the size if wordwrap is on ---------------------------------------------------------------------- Zvoni · 2023-06-26 15:27 Warfley, err.... i'll take your word for it, since the scenario you described.....well..... ;-) ---------------------------------------------------------------------- domasz · 2023-06-26 15:43 Quote from: paweld on June 26, 2023, 03:38:02 pm@Zvoni - will not work well if the label has the WordWrap property set to True. Great answer, thank you. ---------------------------------------------------------------------- domasz · 2023-06-26 15:46 Quote from: Zvoni on June 26, 2023, 05:27:10 pmWarfley, err.... i'll take your word for it, since the scenario you described.....well..... ;-) I have Wordwrap := True and here's my scenario: I have a big panel, inside this panel I have an image (I) an a label (L). The width of the panel and the size of the image I are constant. So the width of the label L cannot change and if there's too much text in it I need it to grow higher and then the whole panel to grow higher.