Page 1 of 1

Word Wrap title

Posted: Sat Feb 19, 2011 7:44 pm
by 10546565
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

Re: Word Wrap title

Posted: Tue Feb 22, 2011 4:43 am
by 10546565
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)

Re: Word Wrap title

Posted: Wed Feb 23, 2011 10:27 am
by 10050769
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,

Re: Word Wrap title

Posted: Wed Feb 23, 2011 3:54 pm
by 10546565
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

Re: Word Wrap title

Posted: Fri Feb 25, 2011 3:43 pm
by 10050769
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,