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

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
simon.schvartzman
Posts: 665
Joined: Tue Jul 29, 2014 12:52 am

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

Post by simon.schvartzman » Fri Oct 25, 2019 4:27 pm

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
Simon
________________________________________
To ";" or not to ";" that is the question

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

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

Post by jmburnod » Fri Oct 25, 2019 5:07 pm

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
https://alternatic.ch

Klaus
Posts: 14208
Joined: Sat Apr 08, 2006 8:41 am
Contact:

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

Post by Klaus » Fri Oct 25, 2019 5:19 pm

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)! :-)

simon.schvartzman
Posts: 665
Joined: Tue Jul 29, 2014 12:52 am

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

Post by simon.schvartzman » 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.

Many thanks!
Simon
________________________________________
To ";" or not to ";" that is the question

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10349
Joined: Wed May 06, 2009 2:28 pm

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

Post by dunbarx » Fri Oct 25, 2019 5:29 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10057
Joined: Sat Apr 08, 2006 7:05 am
Contact:

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

Post by FourthWorld » Fri Oct 25, 2019 7:06 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

simon.schvartzman
Posts: 665
Joined: Tue Jul 29, 2014 12:52 am

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

Post by simon.schvartzman » Fri Oct 25, 2019 7:45 pm

Many thanks Richard!
Simon
________________________________________
To ";" or not to ";" that is the question

Post Reply