Word Wrap title

TeeChart VCL for Borland/CodeGear/Embarcadero RAD Studio, Delphi and C++ Builder.
Post Reply
TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Word Wrap title

Post by TestAlways » Sat Feb 19, 2011 7:44 pm

Is it possible to automatically word wrap a title? I have a fairly long one a custom wants on a smaller chart. If it can't, but there is a code-snippet around for inserting the #13s into the text, I would appreciate it.

Thank you,
Ed Dressel

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: Word Wrap title

Post by TestAlways » Tue Feb 22, 2011 4:43 am

I used the following code, but i my app, had to use 60% of the chart's width to get it t work correctly. (The chart is created just for an image and I had to parent it with a form for it to work).

Code: Select all

function GetWrappedText(const aWidth: integer; aCanvas: TCanvas3D; const aText: string): string;
var
  lFinished: Boolean;
  lPos: Integer;
  lText: string;
  lUnusable: string;
begin
  result := '';

  lText := aText;
  lUnusable := '';
  while Length(lText) > 0 do
  begin
    lFinished := False;
    while (aCanvas.TextWidth(lText) > aWidth) and (not lFinished) do
    begin
      lPos := ScanB(lText, #32, Length(lText));
      if (lPos > 0) then
      begin
        lUnusable := Trim(Copy(lText, lPos + 1, MaxInt) + ' ' + lUnusable);
        Delete(lText, lPos, MaxInt);  
      end
      else
        lFinished := True;
    end;
    result := result + lText;
    if Length(lUnusable) > 0 then
      result := result + #13;
    lText := lUnusable;
    lUnusable := '';
  end;
end;

Maybe someone has a better code snippet that would work better? (BTW, I am using version 8.06 of the chart)

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Word Wrap title

Post by Sandra » Wed Feb 23, 2011 10:27 am

Hello TestAlways,

If you change the way of calculating the position where you want to cut text, using number of characters instead of measuring pixels you can achieve that easily using Delphi’s TextWrap function.

On the other hand, I couldn’t run your code because method ScanB doesn’t compile here. So, you can try to arrange a simple example that we can run as-is to reproduce the problem here?

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

TestAlways
Advanced
Posts: 228
Joined: Tue Aug 28, 2007 12:00 am
Location: Oregon, USA

Re: Word Wrap title

Post by TestAlways » Wed Feb 23, 2011 3:54 pm

Sorry--ScanB is a Hyper Strings function. Here is a function that works for this scenario (FWIW, it is not from the HyperStr.pas file, I just quickly wrote this):

Code: Select all

function ScanB(const aText: string; const aChar: char; const aStartPos: integer): integer;
var
  I: Integer;
begin
  for I := Min(aStartPos, Length(aText)) downto 0 do
    if aText[I] = aChar then
    begin
      result := I;
      exit;
    end;
  result := 0;
end;
That will allow the previous code to run.

Ed Dressel

Sandra
Site Admin
Site Admin
Posts: 3132
Joined: Fri Nov 07, 2008 12:00 am

Re: Word Wrap title

Post by Sandra » Fri Feb 25, 2011 3:43 pm

Hello Dressel,

Sorry for the delay. I have tested your code and it seems work fine for me using last version 8 of TeeChartVCL. Please, could you explain exactly which problem you see when use your code, because we can try to help you solve it?

Thanks,
Best Regards,
Sandra Pazos / Development & Support
Steema Software
Avinguda Montilivi 33, 17003 Girona, Catalonia
Tel: 34 972 218 797
http://www.steema.com
Image Image Image Image Image Image
Instructions - How to post in this forum

Post Reply