2

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  := [];

Screenshot 1

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;

      scrollPosition := scrollPosition + trunc(currentBlurb.Height) + 50;

Screenshot 2

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

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
dddaaammmooo
  • 21
  • 1
  • 4
  • It looks like your screenshots are reversed. – Graymatter May 05 '14 at 05:07
  • thanks, flipped them around – dddaaammmooo May 05 '14 at 05:10
  • 4
    I think to get a wrapped TLabel I usually turn off Autosize, set Wordwrap true and drag the label bounds to the max required layout size. Set any alignment anchors etc. Only then will it behave with Wordwrap – Brian Frost May 05 '14 at 05:27
  • @BrianFrost, that looks like it should be an answer. – Johan May 05 '14 at 16:09
  • I tried playing with the TLabel in the design view. Started by turning AutoSize to False, set Width to 200, Text to 'This is a test This is a test This is a test This is a test This is a test', then set WordWrap to True (no change in size), then set AutoSize to True (width expands to string length but no change in height- GRRR). I then tried doing the exact same on a VCL form and it works fine, so it definitely seems to be something specific to the FMX TLabel component. – dddaaammmooo May 05 '14 at 23:02
  • Interestingly I just noticed in the FMX form, as soon as I turn AutoSize on, WordWrap turns off- seems like they're mutually exclusive. – dddaaammmooo May 05 '14 at 23:04
  • Okay, so after more than a week of trying, even downloading and playing with Xamarin Studio and other development tools to see if would be easier to build the app in that I finally found a solution! Forget using TLabel, WordWrap works fine in VCL, but just doesn't work as documented in FMX forms. Instead I created the control as a TText and everything works beautifully just by setting WordWrap and AutoSize to true. There goes a week of my life I'm never getting back! – dddaaammmooo May 07 '14 at 03:12

0 Answers0