Page 1 of 1

How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 4:27 pm
by simon.schvartzman
Hi experienced developers, I have this "fixed width, height" label field to which before setting its contents (at run time) I would like to know if the text is going to fit on it.

In other words I would like to avoid to set the contents to a text that will imply that the user will need need to scroll the field in order to be able to read it.

The text could have variable length and line breaks...

I can't figure out how to handle it.

Hope I made myself clear.

Thanks

Re: How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 5:07 pm
by jmburnod
Hi Simon,
Not sure i understand you correctly.
I you need to display all the text in a field with width fixe, you may use formattedHeight

To know if you can display your text in your fld:

Code: Select all

 put the height of fld "myField" >= ((the formattedHeight of fld "myField") + (the margins of fld  "myField"))
A quick and dirty way to resize the fld:

Code: Select all

   ...
   lock screen	
   put the topleft of fld "myField" into tTL
   set the height of fld "myField" to ((the formattedHeight of fld "myField") + (the margins of fld  "myField"))
   set the topleft of fld "myField" to tTL
   ...
Best regards
Jean-Marc

Re: How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 5:19 pm
by Klaus
Hi Simon,

what Jean-Marc said does also apply to buttons, so this worked fine in my little test:
Width of button -> 82 px
Label of button -> Button, very loooooooooong
Script:

Code: Select all

on mouseUp pMouseButton
   set the width of me to the formattedwidth of me
end mouseUp
Et voila, it fits (new width 149 px)! :-)

Re: How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 5:21 pm
by simon.schvartzman
Jean-Marc & Klaus, instead of resizing the object what I want to do is to resize its contents in case it doesn't fit. Your answer will certainly help to achieve my goal.

Many thanks!

Re: How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 5:29 pm
by dunbarx
Try something like this. make two fields. Put a lot of text into fld 2, so that it will not fit into fld 1. Put this in a button script;

Code: Select all

on mouseUp
   put the height of fld 1 into origHeight
   put fld 2 into fld 1
   repeat until the formattedHeight of fld 1 <= origHeight
      set the textSize of fld 1 to the textSize of fld 1 - 1
   end repeat
end mouseUp
Craig

Re: How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 7:06 pm
by FourthWorld
simon.schvartzman wrote:
Fri Oct 25, 2019 5:21 pm
Jean-Marc & Klaus, instead of resizing the object what I want to do is to resize its contents in case it doesn't fit. Your answer will certainly help to achieve my goal.
There was a thread about this on the use-livecode list recently:
https://www.mail-archive.com/use-liveco ... 04277.html

One thing that didn't come up there was what happens when the text is so long that reducing it to fit the object impairs readability.

But if that's not a need for your situation, the solutions offered toward the end of the thread may be helpful.

Re: How to know if a text will fit on a label field?

Posted: Fri Oct 25, 2019 7:45 pm
by simon.schvartzman
Many thanks Richard!