3

I create a TLabel and add text to it dynamically, and that works just fine. But I need to know if the label word-wrapped the text because it was too large for the label's width, or if there was no need to it to wrap because the text was shorter than the label's width.

I don't mean to know whether the label's WordWrap property is set to true or false, I want to know if it wrapped the text or it didn’t.

The TLabel has the AutoSize and WordWrap properties set to true, in case that matters.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Hiram
  • 159
  • 1
  • 9
  • The only way to do this is to manually calculate the text width the same way the `TLabel` does - using the Win32 [`DrawText()`](https://learn.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-drawtext) function (which is wrapped by the `TCanvas.TextRect()` overload that takes a `TTextFormat` as input). – Remy Lebeau Mar 15 '19 at 21:55
  • Unless that label is Aligned the wrapping will not work because it is **AutoSized** thus it will always change its size to fit the text in it – Nasreddine Galfout Mar 15 '19 at 22:22
  • thanks for pointing that option Remy, I'll look into that and see how it goes and let you know. – Hiram Mar 15 '19 at 22:49
  • yeah, you are right Nasreddine, I made a mistake there. I am no longer setting the label to autoWidth. – Hiram Mar 15 '19 at 22:51
  • 1
    Next time when you want to respond to someone use the '@' char followed by their name, Neither @Remy Nor I got notified of your comments. have a good weekend :). – Nasreddine Galfout Mar 15 '19 at 23:28
  • thanks for the tip @NasreddineGalfout! I am still noob to the platform haha. much appreciated. – Hiram Mar 19 '19 at 15:32
  • @NasreddineGalfout I didn’t get to try your answer, I'm sorry. I tried the code provided by Sertac and it worked as desired and it was pretty simple. is there any way I can Help you for answering? like voting you up or something? – Hiram Mar 19 '19 at 18:56
  • haha I did not Answer, I meant that you should accept Sertac's answer. you made me smile thank you for that. – Nasreddine Galfout Mar 19 '19 at 19:30
  • haha, I got it now. does that mean that I haven't accepted Sertac's answer yet? I think I did hahaha. could you confirm that @NasreddineGalfout? – Hiram Mar 19 '19 at 20:52
  • yes you did, that green check confirms this :) – Nasreddine Galfout Mar 19 '19 at 21:35

1 Answers1

7

You can check the label's height to find out if it is a one liner or not.

if Label1.Height = Label1.Canvas.TextHeight('.') then
  // no word wrapped

That is because:

When WordWrap is True, the width of the label is fixed. If AutoSize is also True, changes to the text cause the label to change in height.

Sertac Akyuz
  • 54,131
  • 4
  • 102
  • 169
  • that sounds helpful, as im already going home from work I'll try your answer on Monday and I'll let you know how it goes. thanks! – Hiram Mar 15 '19 at 22:46
  • 4
    Ok. If anyone doubts if height of a "." (or any single character) could be tested against the height of an arbitrary string, it could. GetTextExtentPoint32 does not differentiate height of characters. – Sertac Akyuz Mar 15 '19 at 22:51