Advertisement
Borrisholt

cxScalableLabel

Feb 23rd, 2015
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.88 KB | None | 0 0
  1. unit cxScalableLabel;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes, Graphics,
  7.   cxControls, cxLabel, cxGraphics;
  8.  
  9. type
  10.   TcxScalableLabel = class(TcxLabel, IcxLockedStateFontChanged)
  11.   protected
  12.     procedure FontChanged(AFont: TFont);
  13.     procedure TextChanged; override;
  14.     procedure Resize; override;
  15.     function CalculateMazSize: Integer;
  16.   public
  17.     constructor Create(aOwner: TComponent); override;
  18.   end;
  19.  
  20. implementation
  21.  
  22. uses
  23.   Windows, Math;
  24. { TcxScalableLabel }
  25.  
  26. function TcxScalableLabel.CalculateMazSize: Integer;
  27.   function LargestFontSizeToFitWidth: Integer;
  28.   var
  29.     Font: TFont;
  30.     FontRecall: TFontRecall;
  31.     InitialTextWidth: Integer;
  32.   begin
  33.     Font := Canvas.Font;
  34.     Result := Font.Size;
  35.     FontRecall := TFontRecall.Create(Font);
  36.     try
  37.       InitialTextWidth := Canvas.TextWidth(Caption);
  38.       Font.Size := MulDiv(Font.Size, Width, InitialTextWidth);
  39.  
  40.       if InitialTextWidth < Width then
  41.         while True do
  42.         begin
  43.           Font.Size := Font.Size + 1;
  44.           if Canvas.TextWidth(Caption) > Width then
  45.             exit(Font.Size - 1);
  46.         end;
  47.  
  48.       if InitialTextWidth > Width then
  49.       begin
  50.         while True do
  51.           Font.Size := Font.Size - 1;
  52.         if Canvas.TextWidth(Caption) <= Width then
  53.           exit(Font.Size);
  54.       end;
  55.     finally
  56.       FontRecall.Free;
  57.     end;
  58.   end;
  59.  
  60.   function LargestFontSizeToFitHeight: Integer;
  61.   var
  62.     Font: TFont;
  63.     FontRecall: TFontRecall;
  64.     InitialTextHeight: Integer;
  65.   begin
  66.     Font := Canvas.Font;
  67.     Result := Font.Size;
  68.     FontRecall := TFontRecall.Create(Font);
  69.     try
  70.       InitialTextHeight := Canvas.TextHeight(Caption);
  71.       Font.Size := MulDiv(Font.Size, Height, InitialTextHeight);
  72.  
  73.       if InitialTextHeight < Height then
  74.         while True do
  75.         begin
  76.           Font.Size := Font.Size + 1;
  77.           if Canvas.TextHeight(Caption) > Height then
  78.             exit(Font.Size - 1);
  79.         end;
  80.  
  81.       if InitialTextHeight > Height then
  82.         while True do
  83.         begin
  84.           Font.Size := Font.Size - 1;
  85.           if Canvas.TextHeight(Caption) <= Height then
  86.             exit(Font.Size);
  87.         end;
  88.  
  89.     finally
  90.       FontRecall.Free;
  91.     end;
  92.   end;
  93.  
  94. begin
  95.   if Caption <> '' then
  96.     Result := Min(LargestFontSizeToFitWidth, LargestFontSizeToFitHeight)
  97.   else
  98.     Result := Style.Font.Size;
  99. end;
  100.  
  101. constructor TcxScalableLabel.Create(aOwner: TComponent);
  102. begin
  103.   inherited;
  104.   FontListenerList.Add(Self);
  105. end;
  106.  
  107. procedure TcxScalableLabel.FontChanged(AFont: TFont);
  108. var
  109.   NewSize: Integer;
  110. begin
  111.   NewSize := CalculateMazSize;
  112.   if AFont.Size <> NewSize then
  113.     Style.Font.Size := NewSize;
  114. end;
  115.  
  116. procedure TcxScalableLabel.Resize;
  117. begin
  118.   inherited;
  119.   Style.Font.Size := CalculateMazSize;
  120. end;
  121.  
  122. procedure TcxScalableLabel.TextChanged;
  123. begin
  124.   inherited;
  125.   Style.Font.Size := CalculateMazSize;
  126. end;
  127.  
  128. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement