Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the font of a Delphi TLabel to Italic chops off the tail - why?

A simple demo of a default TLabel with font set to Arial Regular 16 is shown below. enter image description here

The code when the button is clicked is:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Font.Style := Label1.Font.Style + [fsItalic];
end;

When you click the button, the last character is truncated viz:

enter image description here

By default, TLAbel.AutoSize is true so this should be ok, right? This is in XE and Delphi 7 is the same. Is this a bug?

like image 659
Brian Frost Avatar asked May 26 '11 15:05

Brian Frost


People also ask

How do I change the font on my Delphi code?

You can change the editor font and size in the Options menu. From Help: Select a font type from the available screen fonts installed on your system (shown in the list). The Code Editor displays and uses only monospaced screen fonts, such as Courier.

What is the font style of italic?

Italic is a type style that's almost always slanted and is designed to create emphasis in text. Originally based on semi-cursive forms, italics are a direct contrast to the upright style.

How do you change the font to bold in Delphi?

Try this: Label1. Font. Style := [fsBold];


2 Answers

An extra space at the end is a quick work around for this.

like image 82
Tim Avatar answered Oct 20 '22 07:10

Tim


Yes, it would seem so (although a rather minor bug). Possible work-arounds include

  • drawing the text yourself, using the Windows API function TextOut (or DrawText), and
  • using a TStaticText (instead of a TLabel), which is merely a wrapper for a Windows static control (in text mode). Of course, Windows draws the text correctly.

Using TextOut

procedure TForm4.FormPaint(Sender: TObject);
const
  S = 'This is a test';
begin
  TextOut(Canvas.Handle,
    10,
    10,
    PChar(S),
    length(S));
end;

TextOut sample

Using a static control (TStaticText)

Static control sample

I would guess that this is not a problem in the Microsoft Windows operating system, but only in the VCL TLabel control.

Update

I tried

procedure TForm4.FormPaint(Sender: TObject);
const
  S = 'This is a test';
var
  r: TRect;
begin
  r.Left := 10;
  r.Top := 10;
  r.Bottom := r.Top + DrawText(Canvas.Handle,
    PChar(S),
    length(S),
    r,
    DT_SINGLELINE or DT_LEFT or DT_CALCRECT);
  DrawText(Canvas.Handle,
    PChar(S),
    length(S),
    r,
    DT_SINGLELINE or DT_LEFT);
end;

and the result is this:

DrawText sample

Thus, this is a problem in the Microsoft Windows operating system (or the Arial font), after all.

A workaround is to add the DT_NOCLIP flag:

procedure TForm4.FormPaint(Sender: TObject);
const
  S = 'This is a test';
var
  r: TRect;
begin
  r.Left := 10;
  r.Top := 10;
  r.Bottom := r.Top + DrawText(Canvas.Handle,
    PChar(S),
    length(S),
    r,
    DT_SINGLELINE or DT_LEFT or DT_CALCRECT);
  DrawText(Canvas.Handle,
    PChar(S),
    length(S),
    r,
    DT_SINGLELINE or DT_LEFT or DT_NOCLIP);
end;

DrawText with DT_NOCLIP sample

Update 2

A light-weight fix might be

type
  TLabel = class(StdCtrls.TLabel)
  protected
    procedure DoDrawText(var Rect: TRect; Flags: Integer); override;
  end;

...

{ TLabel }

procedure TLabel.DoDrawText(var Rect: TRect; Flags: Integer);
begin
  inherited;
  if (Flags and DT_CALCRECT) <> 0 then
    Rect.Right := Rect.Right + 2;
end;

yielding the result

TLabel with slight modification

(But hard-coding a magic value (2) seems nasty...)

like image 22
Andreas Rejbrand Avatar answered Oct 20 '22 06:10

Andreas Rejbrand