Search code examples
textfieldlivecodetext-formatting

Dynamic text size resizing to fit the entire text line into a text field


I have about 640 text lines of different length that can be viewed both as a list in a data grid and in a one-line view. In one-line view I'd like to resize the font size of the text line so that it

  1. fits completely in the text field

  2. has maximum point size 24 and minimum point size 10 (I have set the custom properties for that in the text filed).

I'm using the following code in the one-line view card:

on resizeText

 ## Check if the text fits in the field
 put the textSize of fld "foneline" into tTextSize
 if the formattedHeight of fld "foneline" > the height of fld "foneline" then

 ## Make the text smaller until it fits
 repeat until the formattedHeight of fld "foneline" <= the height of fld "foneline" 
   subtract 1 from tTextSize

  ## Check that the text size is not less that the minimum size       
  if tTextSize >= the cMinimumTextSize of fld "foneline" then     
       set the textSize of fld "foneline" to tTextSize      
  else         
   exit repeat       
  end if    
  end repeat  
 else if the formattedHeight of fld "foneline" < the height of fld "foneline" then

  ## Make the text as large as possible    
 repeat until the formattedHeight of fld "foneline" >= the height of fld "foneline"      
   add 1 to tTextSize         

  ## Check that the text size is not bigger that the maximum size      
   if tTextSize<= the cMaximumTextSize of fld "foneline" then       
     set the textSize of fld "foneline" to tTextSize      
  else        
    exit repeat       
  end if     
 end repeat  
 end if
end resizeText

and the resizeText command in the code for Previous and Next buttons on that card.

There is however some glitch or fault in the code that shows up which I hope you can replicate by following these steps:

  1. open the stack

  2. click on "Select All" button

  3. click on "Lines" button

  4. click on "Next" button until you see line No. 6 (it's visible in its full length).

  5. click on "Next" button again and you'll see line 7 but the line is not fully visible (you'll see that the closing quotation mark " is not there)

  6. click on "Next" button and then on "Previous" button - this will take you back to the line 7 which now is visible in its full length!

  7. now click on "Previous" button and line 6 is visible but this time not in its full length

  8. Click on "Previous" and again on "Next" and now the line 6 is in its full length

  9. click on "Next" and line 7 is again not visible completely.

If you are not able to replicate this with the line numbers mentioned above try to continue clicking on the "Next" button until you come across a line that is missing the end " quotation mark which indicates that the line is cut off. Then follow the same procedure of clicking on "Previous" and "Next" buttons as above. It may sound complicated but once you open the stack it's simple to find out the faulty behaviour. Similar is the case with lines: 83-84-85; 48-49; 51-52. The link to my stack is DG-only-1.16_cut-off-lines.zip

How to correct the code so that it always shows each line without cutting off the end of it?


Solution

  • In your 'resizeText' handler: if the formatted height is smaller than the field, then you resize the text larger until it's >= the formattedHeight. This means that the loop sometimes stops when it's height is greater than required. By adding a final height check when the font size is increased, we can avoid this;

    on resizeText
       ## Check if the text fits in the field
       put the textSize of fld "foneline" into tTextSize
       put the height of fld "foneline"  into tHeight
    
       if the formattedHeight of fld "foneline" > tHeight then
          ## Make the text smaller until it fits
          repeat until the formattedHeight of fld "foneline" <= tHeight 
             subtract 1 from tTextSize
    
             ## Check that the text size is not less that the minimum size
             if tTextSize >= the cMinimumTextSize of fld "foneline" then
                set the textSize of fld "foneline" to tTextSize
             else
                exit repeat
             end if
          end repeat      
       else if the formattedHeight of fld "foneline" < tHeight then
          ## Make the text as large as possible
          repeat until the formattedHeight of fld "foneline" >= tHeight
             add 1 to tTextSize
    
             ## Check that the text size is not bigger that the maximum size
             if tTextSize<= the cMaximumTextSize of fld "foneline" then
                set the textSize of fld "foneline" to tTextSize
             else
                exit repeat
             end if
          end repeat
    
          # check the final field height
          if the formattedHeight of fld "foneline" > tHeight then
             subtract 1 from tTextSize
             set the textSize of fld "foneline" to tTextSize
          end if
       end if
    end resizeText