rbWiki > Design > Formatting > How To...Format Text Overflow with Ellipse

How To...Format Text Overflow with Ellipse

Table of contents
  1. 1. Question 
  2. 2. Solution

Question 

"How can I format text with ellipse '...' to indicate that it overflows a fixed width control such as a Label?" 

Solution

This example shows uses the detail BeforePrint event to format a Label.Caption with a data field value. When the text is wider than the Label.Width, the text is truncated with a '... on the end. 

Download: FormatTextWithEllipse.zip

Sample Delphi code:

uses
  ppUtils, 
  ppTypes;


procedure TForm1.ppDetailBand1BeforePrint(Sender: TObject);
const
  csEllipses = '...';
var
  ldTextWidth: Double;
  lsText: String;
  lsTextPlusEllipses: String;
begin

  lsText := ppReport1.DataPipeline['Company'];

  ldTextWidth := CalcTextWidthInReportUnits(lsText, ppLabel1.Font);


  {shrink length of text and add the elipses, as needed}
  if (ldTextWidth > ppLabel1.Width) then
    repeat
      lsText := Copy(lsText,1, Length(lsText)-1);
      lsTextPlusEllipses := lsText + csEllipses;
      ldTextWidth := CalcTextWidthInReportUnits(lsTextPlusEllipses, ppLabel1.Font);

    until (ldTextWidth < ppLabel1.Width);

  ppLabel1.Caption := lsTextPlusEllipses;

end;


function TForm1.CalcTextWidthInReportUnits(aText: String; aFont: TFont): Double;
var
  liPrinterPixels: Integer;
  liMicrons: Integer;
begin

  ppReport1.Printer.Canvas.Font := aFont;

  {calc width in printer pixels}
  liPrinterPixels := ppReport1.Printer.Canvas.TextWidth(aText);

  {convert to microns}
  liMicrons := ppToMMThousandths(liPrinterPixels, utPrinterPixels, pprtHorizontal, ppReport1.Printer);

  {convert from microns to report units}
  Result := ppFromMMThousandths(liMicrons, ppReport1.Units, pprtHorizontal, ppReport1.Printer);


end;
Tags
none

Files (0)

 
You must login to post a comment.