Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Delphi XE5 TLabel Word Wrapping


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 3 - Last Post: Jun 3, 2014 8:31 AM Last Post By: Douglas Rudd
Damian Sloane

Posts: 1
Registered: 2/25/14
Delphi XE5 TLabel Word Wrapping  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 6, 2014 5:30 PM
I'm having a hell of a time trying to get word wrapping working correctly with the TLabel component in a Delphi XE5 Firemonkey mobile app. The background is that I am trying to programmatically populate a TScrollBox component on a form with various images and labels, and some of the labels will need to wrap.

Some posts seem to indicate that if you set the components width to a predefined size then make both the AutoSize and WordWrap properties true that the label will wrap and adjust the height as required. Unfortunately in my testing it seems that this doesn't work and that as soon as AutoSize is true the component defaults to a single long line of text.

currentBlurb := TLabel.Create(self);
currentBlurb.parent := scrollNews;
currentBlurb.Width := scrollNews.Width - 50;
currentBlurb.Position.X := 25;
currentBlurb.Position.Y := scrollPosition;
currentBlurb.WordWrap := true;
currentBlurb.AutoSize := true;
currentBlurb.Font.Size := 12;
currentBlurb.Font.Style := [];
currentBlurb.Visible := true;
currentBlurb.Text := resultArticles.O[articleIndex].S['blurb'];
currentBlurb.StyledSettings := [];

[http://i.stack.imgur.com/8kxkO.png]

I even tried playing with the label in the design view and the same behaviour rings true. But this does work for VCL forms! GRRR!

Other posts have suggested using the MeasureText procedure to calculate the height of the wrapped text, however this calculation while close to the correct height doesn't seem to be working for me correctly as it randomly chops off some of the bottom text.

currentBlurb := TLabel.Create(self);
currentBlurb.parent := scrollNews;
currentBlurb.AutoSize := false;
currentBlurb.WordWrap := true;
currentBlurb.Position.X := 25;
currentBlurb.Position.Y := scrollPosition;
currentBlurb.Width := scrollNews.Width - 50;
currentBlurb.Font.Size := 12;
currentBlurb.Font.Style := [];
currentBlurb.Visible := true;
currentBlurb.Text := resultArticles.O[articleIndex].S['blurb'];
currentBlurb.StyledSettings := [];

{ The TLabel component doesn't seem to wrap as the documentation would suggest }
{ so we need to manually calculate the height of the label after wrapping }

rectBlurb := TRectF.Create(0, 0, currentBlurb.Width, 10000);
currentBlurb.Canvas.MeasureText
(
rectBlurb,
currentBlurb.Text,
True,
[],
TTextAlign.taLeading,
TTextAlign.taLeading
);

currentBlurb.Height := rectBlurb.Height;

[http://i.stack.imgur.com/JfUpO.png]

This has been driving me nuts for over a week now, so any assistance would be greatly appreciated.
Linden ROTH

Posts: 467
Registered: 11/3/11
Re: Delphi XE5 TLabel Word Wrapping  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 6, 2014 10:47 PM   in response to: Damian Sloane in response to: Damian Sloane
Damian Sloane wrote:
I'm having a hell of a time trying to get word wrapping working correctly with the TLabel component in a Delphi XE5 Firemonkey mobile app.
The background is that I am trying to programmatically populate a TScrollBox component on a form with various images and labels, and some of the labels will need to wrap.

Some posts seem to indicate that if you set the components width to a predefined size then make both the AutoSize and WordWrap properties true that the label will wrap and adjust the height as required.
Unfortunately in my testing it seems that this doesn't work and that as soon as AutoSize is true the component defaults to a single long line of text.

Just a thought but why not use a readonly TMemo as it does what you want already (just need to adjust the height based on the number of lines)

as to why it doesn't work how you want it to look at the source

FMX.StdCtrls
procedure TLabel.Resize;
begin
  inherited;
  if FAutoSize then
    FitSize;
end;
 
procedure TLabel.FitSize;
begin
  if not (csLoading in ComponentState) then
    WordWrap := False;
  ApplyStyle;
end;

--
Linden
"Mango" was Cool but "Wasabi" was Hotter but remember it's all in the "source"

Gary Wardell

Posts: 241
Registered: 11/13/03
Re: Delphi XE5 TLabel Word Wrapping  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 3, 2014 12:15 AM   in response to: Damian Sloane in response to: Damian Sloane
Damian Sloane wrote:
? it randomly chops off some of the bottom text.


This has been driving me nuts for over a week now, so any assistance would be greatly appreciated.

It drives me nuts too.

One thing, I found that some devices, like Androids, only have a limited number of fonts and font sizes available. So perhaps one needs to determine what font is actually selected on the device to get the size right.
Douglas Rudd

Posts: 314
Registered: 5/16/97
Re: Delphi XE5 TLabel Word Wrapping  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 3, 2014 8:31 AM   in response to: Damian Sloane in response to: Damian Sloane
I have done this in a scrollbox. The main thing I had to do is set the font for Canvas.MeasureText because otherwise it uses the wrong font size for its calculations.
 s := mylabel.Text;
  ARect.Left := 0;
  ARect.Top := 0;
  ARect.Width := label_width;
  ARect.Height := 1000;
  mylabel.Canvas.font.Assign(mylabel.font);
  mylabel.Canvas.MeasureText(ARect, s, true, [], TTextAlign.taLeading, TTextAlign.taLeading);
 mylabel.Height := ARect.Height;

Worked the same for IOS and Android.
All my labels are Aligned to Top in the scrollbox. You can put labels inside layouts if you want to do more arraigning on the page.

Sometimes I may have to fine tune it by adding an extra half line height at the end.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02