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 := [];
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;
This has been driving me nuts for over a week now, so any assistance would be greatly appreciated.