My Stupid code 2

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
MaxV
Posts: 1579
Joined: Tue May 28, 2013 2:20 pm
Location: Italy
Contact:

My Stupid code 2

Post by MaxV » Mon May 07, 2018 6:07 pm

Hi,
another problem that I solved wasting a lot of cycles, do you have a better one?
I need to use the bigger font available to fit all the text inside the field.

Code: Select all

put true into ttt
put 1 into t2
repeat while ttt
      add 1 to t2
      set the textsize of field "presentazione" to t2
      if the formattedheight of field "presentazione" > the height of field "presentazione" then
         put false into ttt
         set textsize of field "presentazione" to (t2 - 1)
      end if
 end repeat
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

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

Re: My Stupid code 2

Post by richmond62 » Sun Aug 26, 2018 12:08 pm

That seems to go round in circles until I hit the PANIC button.

"If you specify a field, the formattedWidth reports the width required by the field's text."

As the textSize increases so does the formattedWidth. :evil:

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

Re: My Stupid code 2

Post by richmond62 » Sun Aug 26, 2018 12:16 pm

textFit.png
-
this semi-works, in so far as it has a problem with wrapping test:

Code: Select all

on mouseUp
   put 0 into DUN
   put the textSize of fld "f1" into BIGG
   repeat until DUN > 1
      if the formattedWidth of fld "f1" > ((the width of fld "f1") - 1) then
         add 1 to DUN
      end if
      if the formattedHeight of fld "f1" > ((the height of fld "f1") - 1) then
         add 1 to DUN
      end if
      add 1 to BIGG
      set the textSize of fld "f1" to BIGG
   end repeat
end mouseUp
-
textFit2.png
-
Text Fitter.livecode.zip
Here's the stack.
(1.26 KiB) Downloaded 168 times
Last edited by richmond62 on Mon Aug 27, 2018 8:04 am, edited 1 time in total.

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

Re: My Stupid code 2

Post by richmond62 » Sun Aug 26, 2018 12:21 pm

If the field is set to dontWrap ones gets another stupid effect:
-
textFit3.png
-
wrapCrap.png

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

Re: My Stupid code 2

Post by richmond62 » Sun Aug 26, 2018 2:08 pm


bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: My Stupid code 2

Post by bogs » Sun Aug 26, 2018 3:02 pm

Mind you, I haven't tested any of this yet (still working on my first cup of coffee Image ) but...

Wouldn't comparing the formatted width to the width of the field/label/etc like Richmond put there be the way to go? Something like that, but increase the subtraction level until it fits like your looking for -

Code: Select all

if the formattedWidth of field "xxx" > (the width of field "xxx" -5) then  
for instance?
Image

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

Re: My Stupid code 2

Post by richmond62 » Sun Aug 26, 2018 3:49 pm

Because the "subtraction level" would depend both on font size and word lengths.

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

Re: My Stupid code 2

Post by jmburnod » Sun Aug 26, 2018 5:02 pm

Hi All,

This seems work:

Code: Select all

on mouseUp
   put the short id of fld "f1" into tFldID
   doSetTextSize tFldID,100,the height of fld "f1", the margins of fld "f1"
   put the textsize of char 1 of fld "f1"
end mouseUp

on doSetTextSize pFldID,pTextSize,pHeightPoss,pMargin
   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 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
Jean-Marc
https://alternatic.ch

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7215
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: My Stupid code 2

Post by jacque » Sun Aug 26, 2018 10:01 pm

I just tried MaxV's original handler and it seems to work fine, and is probably the shortest way to do it. The only problem I had was a sudden crash after the handler ended using LC 9.0.1(rc2) but I don't think that's the handler's fault. It may have been because I had other stacks open at the same time which may have interfered.

I wouldn't start the text at size 1 though, I'd start with something larger, maybe the effective textsize of the stack. Or alternately, see if the current size is too large and subtract from there, otherwise start with the current size and add up from there.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”