Dynamically calculate TppRichText.Width
Hi !
I fill a TppRichText and then calculate it's minimal Width without breaking
the text in two lines.
Example:
"Customs taxes:" must show
"Customs taxes:"
and not
"Customs
taxes:"
I use the font 'Times New Roman', size 10 and style [fsBold]. All the text
is taRightJustify.
How I can calculate this ? I made this function:
function GetLineWidth(Text: String; aFont: TFont): Integer;
var
TempLabel: TLabel;
TempWidth: Integer;
begin
TempLabel:= TLabel.Create(Application.MainForm);
try
TempLabel.Parent:= Application.MainForm;
TempLabel.Font:= aFont;
TempWidth:= TempLabel.Canvas.TextWidth(Text);
Result:= TempWidth + 7;
finally
TempLabel.Free;
end;
end;
Using this function, I extract the longest line of my TppRichText and then
calculate it's Width and then adjust the TppRichText.Width to accomodate the
text. I know that a TppRichText and a TLabel aren't the same, but if both
shows the same text with the same font, the text always uses the same
space... Right ? It seems not. I think the taRightJustify is the source of
my problem. Anyone have an idea ? I searched all day long making plenty of
tests and got nothing...
Anyone can help me !?
Thanks a lot !
Alex.
I fill a TppRichText and then calculate it's minimal Width without breaking
the text in two lines.
Example:
"Customs taxes:" must show
"Customs taxes:"
and not
"Customs
taxes:"
I use the font 'Times New Roman', size 10 and style [fsBold]. All the text
is taRightJustify.
How I can calculate this ? I made this function:
function GetLineWidth(Text: String; aFont: TFont): Integer;
var
TempLabel: TLabel;
TempWidth: Integer;
begin
TempLabel:= TLabel.Create(Application.MainForm);
try
TempLabel.Parent:= Application.MainForm;
TempLabel.Font:= aFont;
TempWidth:= TempLabel.Canvas.TextWidth(Text);
Result:= TempWidth + 7;
finally
TempLabel.Free;
end;
end;
Using this function, I extract the longest line of my TppRichText and then
calculate it's Width and then adjust the TppRichText.Width to accomodate the
text. I know that a TppRichText and a TLabel aren't the same, but if both
shows the same text with the same font, the text always uses the same
space... Right ? It seems not. I think the taRightJustify is the source of
my problem. Anyone have an idea ? I searched all day long making plenty of
tests and got nothing...
Anyone can help me !?
Thanks a lot !
Alex.
This discussion has been closed.
Comments
What are you getting for your return value of the function below? At first
glance, it looks as though it should work. Remember that when using the
TCanvas.TextWidth function, you are getting a Pixel value for the width of
the text. When you set the width of the TppRichText component, it will be
in ReportUnits. Below is a snip of how I might do this:
uses
ppUtils, ppTypes;
var
lBitmap: TBitmap;
liWidth: Integer;
begin
lBitmap := TBitmap.Create;
try
lBitmap.Canvas.Font:= aFont;
liWidth:= lBitmap.Canvas.TextWidth(aText);
Result:= ppFromScreenPixels(liWidth, utMMThousandths,
pprtHorizontal, nil);
finally
lBitmap.Free;
end;
end;
- Then set the TppRichText using the TppRichText.mmWidth property.
--
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com
The units used in my report are utScreenPixels and the return value on my
function is in pixels.
The problem is that it seems that a text in a TppRichText with text set to
taRightJustify needs a blank space somewhere (left or right, I don't know)
to fit on one line... Maybe if I had the code behind the WordWarp feature I
could know...
I'm rally lost ...
Alex.
ReportBuilder uses a wrapper around Delphi's TRichEdit component to display
RichText. First try to get this working in Delphi before you do it in
ReportBuilder. The word wrapping and spacing are the same in ReportBuilder
as they are in Delphi.
--
Nico Cizik
Digital Metaphors
http://www.digital-metaphors.com