Recent

Author Topic: [Solved] How to get vertical text in TLabel descendant  (Read 2006 times)

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
[Solved] How to get vertical text in TLabel descendant
« on: May 08, 2019, 10:32:46 pm »
Using Font.Orientation = 900 in a TLabel provides nice vertical text. I have a TLabel descendant that allows different characters in the Caption to be shown in different colors and fonts. This was easy as I just over-rode the Paint procedure and used Canvas.TextOut.  How can I achieve vertical text in my own component?
« Last Edit: May 09, 2019, 05:15:12 pm by bobonwhidbey »
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 13491
Re: How to get vertical text in TLabel descendant
« Reply #1 on: May 08, 2019, 11:58:46 pm »
Do you need a full-featured "label", i.e. with WordWrap, AutoSize, OptimalFill, or do you just need vertical text? Do you only need vertical text, or also intermediate angles?

The basic idea is to override the Paint procedure. But you must adjust the text starting point positions which is in the top/left corner; you must correct text width and height if you want to draw at a different angle. And the TLabel is so stupid that it does not adjust the clipping rect accoring to the text direction. Multiline text needs also consideration because the canvas always increments y from line to line, but it should increment x in case of vertical text.

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: How to get vertical text in TLabel descendant
« Reply #2 on: May 09, 2019, 02:30:33 am »
No wordwrap, autosize or OptimalFill. Just plan text shown vertically, similar to what's seen on the attached image. BUT I want different colors and fonts for some letters in the string.

I thought about using an image and then showing it turned 90 degrees. But my app is translated into a dozen languages and that would balloon the number of images to several hundred.

Does DrawText have some capabilities that would help?
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 13491
Re: How to get vertical text in TLabel descendant
« Reply #3 on: May 09, 2019, 10:09:21 am »
Since I don't know how you want this to be integrated in a component here is just an example with TPaintbox. In a component you would put the drawing code into the Paint method:
Code: Pascal  [Select][+][-]
  1. uses
  2.   Types, Variants;
  3.  
  4. procedure DrawColoredText(ACanvas: TCanvas; x, y: Integer;
  5.   const AParts: array of const);
  6.   // The array contains color and text: the color parameter sets the color for painting the next text element.
  7. var
  8.   i: Integer;
  9.   s: String;
  10. begin
  11.   for i := 0 to High(AParts) do begin
  12.     if AParts[i].VType = vtInteger then
  13.       ACanvas.Font.Color := AParts[i].VInteger
  14.     else begin
  15.       if AParts[i].VType = vtChar then
  16.         s := AParts[i].VChar
  17.       else
  18.       if AParts[i].VType = vtAnsiString then
  19.         s := PChar(AParts[i].VString)
  20.       else
  21.         Continue;
  22.       ACanvas.TextOut(x, y, s);
  23.       if ACanvas.Font.Orientation = 0 then           // Assume only 0° and 90°!
  24.         inc(x, ACanvas.TextWidth(s))
  25.       else
  26.         dec(y, ACanvas.TextWidth(s));
  27.     end;
  28.   end;
  29. end;
  30.  
  31. procedure TForm1.PaintBox1Paint(Sender: TObject);
  32. const
  33.   RedText = 'Hello';
  34.   BlueText = 'World';
  35. var
  36.   s: String;
  37.   sz: TSize;
  38.   x, y: Integer;
  39. begin
  40.   with Paintbox1 do begin
  41.     Canvas.Brush.Color := clWhite;
  42.     Canvas.FillRect(0, 0, Width, Height);
  43.     Canvas.Font.Assign(Font);
  44.     Canvas.Font.Orientation := 900;
  45.     s := RedText + ' ' + BlueText;
  46.     // Calculate text extent
  47.     sz := Canvas.TextExtent(s);
  48.     // Calculate starting point for text centered within the PaintBox
  49.     x := (Width - sz.CY) div 2;
  50.     y := (Height + sz.CX) div 2;
  51.     // Draw colored text
  52.     DrawColoredText(Canvas, x, y, [clRed, RedText, ' ', clBlue, BlueText]);
  53.   end;
  54. end;

bobonwhidbey

  • Hero Member
  • *****
  • Posts: 630
    • Double Dummy Solver - free download
Re: How to get vertical text in TLabel descendant
« Reply #4 on: May 09, 2019, 05:14:57 pm »
Thanks WP. Once again you've come to the rescue. PaintBox meets my needs. I had never used that component before.
Lazarus 4.6 FPC 3.2.2 x86_64-win64-win32/win64

 

TinyPortal © 2005-2018