Use the anchors only on the top and left. Then on the WMExitSizeMove message procedure use this: Label1.Height := (Label1.Height * Height) div OldHeight; and the same for the Width as a scaling system. then use David's answer to update the font with the scaling (use the function in the pasteBin from the OPs comment to the answer). this would work perfectly for a simple scaling system. And if it bothers you when the font does not scale when only the width or height changes then you can stop your label from scaling at that case.
You get this as a result:


the following code translates to what I said.
unit Unit12;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, system.Math;
type
TForm12 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure WMExitSizeMove(var aMessage: TMessage); message WM_ExitSizeMove;
public
{ Public declarations }
end;
var
Form12: TForm12;
OldWidth, OldHeight: Integer;
implementation
{$R *.dfm}
{ TForm12 }
function CalculateMazSize(aCanvas: TCanvas; aText: string; aWidth, aHeight: Integer): Integer;
function LargestFontSizeToFitWidth(aCanvas: TCanvas; aText: string; aWidth: Integer): Integer;
var
Font: TFont;
FontRecall: TFontRecall;
InitialTextWidth: Integer;
begin
Font := aCanvas.Font;
Result := Font.Size;
FontRecall := TFontRecall.Create(Font);
try
InitialTextWidth := aCanvas.TextWidth(aText);
Font.Size := MulDiv(Font.Size, aWidth, InitialTextWidth);
if InitialTextWidth < aWidth then
while True do
begin
Font.Size := Font.Size + 1;
if aCanvas.TextWidth(aText) > aWidth then
exit(Font.Size - 1);
end;
if InitialTextWidth > aWidth then
begin
while True do
begin
Font.Size := Font.Size - 1;
if aCanvas.TextWidth(aText) <= aWidth then
exit(Font.Size);
end;
end;
finally
FontRecall.Free;
end;
end;
function LargestFontSizeToFitHeight(aCanvas: TCanvas; aText: string; aHeight: Integer): Integer;
var
Font: TFont;
FontRecall: TFontRecall;
InitialTextHeight: Integer;
begin
Font := aCanvas.Font;
Result := Font.Size;
FontRecall := TFontRecall.Create(Font);
try
InitialTextHeight := aCanvas.TextHeight(aText);
Font.Size := MulDiv(Font.Size, aHeight, InitialTextHeight);
if InitialTextHeight < aHeight then
while True do
begin
Font.Size := Font.Size + 1;
if aCanvas.TextHeight(aText) > aHeight then
exit(Font.Size - 1);
end;
if InitialTextHeight > aHeight then
while True do
begin
Font.Size := Font.Size - 1;
if aCanvas.TextHeight(aText) <= aHeight then
exit(Font.Size);
end;
finally
FontRecall.Free;
end;
end;
begin
if aText <> '' then
Result := Min(LargestFontSizeToFitWidth(aCanvas, aText, aWidth),
LargestFontSizeToFitHeight(aCanvas, aText, aHeight))
else
Result := aCanvas.Font.Size;
end;
procedure TForm12.FormCreate(Sender: TObject);
begin
OldWidth := Width;
OldHeight := Height;
end;
procedure TForm12.WMExitSizeMove(var aMessage: TMessage);
begin
// scaling
Label1.Height := (Label1.Height * Height) div OldHeight;
Label1.Width := (Label1.Width * Width) div OldWidth;
// Updating font
Label1.Font.Size := CalculateMazSize(Label1.Canvas, Label1.Caption, Label1.Width, Label1.Height);
// Updating old values
OldWidth := Width;
OldHeight := Height;
end;
end.
One problem with this is if the user maximizes the form then it will not work because based on the documentation this message is only sent when the form is resized or moved by the user.
Sent one time to a window, after it has exited the moving or sizing
modal loop. The window enters the moving or sizing modal loop when the
user clicks the window's title bar or sizing border, or when the
window passes the WM_SYSCOMMAND message to the DefWindowProc function
and the wParam parameter of the message specifies the SC_MOVE or
SC_SIZE value. The operation is complete when DefWindowProc returns.