Fit Text to Object?

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Fit Text to Object?

Post by cmhjon » Wed May 18, 2022 5:57 pm

Hi all,

I am trying to resize the text of a text frame (text frame sizes will vary) so that it does not exceed the width or height of the frame. My code starts the text size at 1pt and then increases it by 1pt (via a repeat routine) until the formattedWidth reaches the width of the frame. This works. Then (and if needed), I need the text size reduced until the formattedHeight does not exceed the height of the field. The width portion seems to work but not the height. I'm sure it's something silly I am missing. Here's my code:

Code: Select all

on mouseUp
   put true into ttt
   put 1 into t2
   repeat while ttt
      add 1 to t2
      set the textsize of field "Tester" to t2
      if the formattedWidth of field "Tester" > the width of field "Tester" then
         put false into ttt
         set textsize of field "Tester" to (t2 - 1)
      end if
   end repeat
   put textSize of field "Tester" into t2
   repeat until the formattedHeight of field "Tester" < the height of field "Tester"
      set textSize of field "Tester" to (t2 - 1)
   end repeat
end mouseUp
Anyone have any ideas?

Thank you,
Jon :)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9356
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Fit Text to Object?

Post by richmond62 » Wed May 18, 2022 7:12 pm

By a text frame do you mean something different from a text field?

cmhjon
Posts: 175
Joined: Tue Aug 04, 2015 11:55 am

Re: Fit Text to Object?

Post by cmhjon » Wed May 18, 2022 7:20 pm

Sorry. Yes, a text field.

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Fit Text to Object?

Post by SparkOut » Wed May 18, 2022 10:33 pm

Code: Select all

   --speed up the action and prevent the user seeing strange text size changes by locking the screen
   lock screen
   --if there is nothing in the field then don't let it go into an endless loop increasing the size of nothing ad infinitum
   if the text of field "Tester" is not empty then
      --initialize small to start from
      put 1 into tSize
      set the textSize of field "Tester" to tSize
      --let the repeat loop end automatically by checking when the text exceeds the bounds
      repeat while (the formattedWidth of field "Tester" < the width of field "Tester") and (the formattedHeight of field "Tester" < the height of field "Tester")
         --increase the size
         add 1 to tSize
         set the textSize of field "Tester" to tSize
         --the loop should be done way before the app looks like it has hung, but I always give a repeat loop a breather
         wait 0 milliseconds with messages
      end repeat
      --the repeat loop exited because the size exceeded bounds, so reduce to the last size that did fit
      set the textSize of field "Tester" to tSize - 1
   end if
   unlock screen

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Fit Text to Object?

Post by jmburnod » Wed May 18, 2022 10:39 pm

Hi All,

I used this:

Code: Select all

on doSetTextSize pFldID,pTextSize,pHeightPoss,pMargin -- pHeightPoss = field height
   put pTextSize into tSizeMax
   set the textsize of char 1 to -1 of fld id pFldID to tSizeMax
   set the fixedlineheight of fld id pFldID to false
   put the width of fld  id pFldID - (pMargin*2) into tMaxW
   put the formattedheight of fld id pFldID into tFH
   if tFH <= pHeightPoss then
      set the height of fld id pFldID to tFH
   end if 
   if tFH > pHeightPoss or  the formattedwidth of fld id pFldID > tMaxW then
      repeat with z = tSizeMax down to 6
         if the optionkey is down then exit repeat --•• just for test
         set the textsize of fld id pFldID to z
         set the  textsize of char 1 to -1 of fld id pFldID to z
         put the formattedheight of fld id pFldID into tFH     
         if tFH <= pHeightPoss and the formattedwidth of fld id pFldID <=tMaxW then 
            exit repeat
         end if
         wait 1 milliseconds with messages
      end repeat
   end if
end doSetTextSize
Best regards
Jean-Marc
https://alternatic.ch

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”