Resizing and Vertically Centering Text in a Field

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
keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Resizing and Vertically Centering Text in a Field

Post by keram » Sat Apr 05, 2014 3:11 am

Hello,

I'm trying to resize and vertically center text in a field.

For text resizing I'm using this code (on the stack level):

Code: Select all

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

and for vertical centering I'm using Bernd's code (on the stack level) - thanks Bernd!:

Code: Select all

   ## centering the text vertically
   put the long id of fld "oneline" into myFld
   put the effective textSize of myFld into tTextSize
   put round (((tTextSize - 22) * 1/3)) into tKorr
   put the formattedheight of char 1 to - 1 of myFld into myFormattedHeight
   put (the height of myFld - myFormattedHeight) / 2 into myVerticalMargins
   put myVerticalMargins - tKorr into tTopMargin
   put myVerticalMargins + tKorr into tBotMargin
   set the margins of myFld to 8,tTopMargin,8,tBotMargin
The displayed text is picked up from a data field with text lines of different length.
I can navigate with buttons (First, Previous, Random, Next and Last - code in the buttons) to display the respective lines.

When clicking on Next or Previous buttons time after time the display is reasonably OK. But when clicking for example on Random button few times it gets unpredictable.

Also when for example clicking buttons in this sequence:
First, Next, Previous (check the display!), Last, First (check the display again!) the display of the 1st line is not the same, not consistent.

How to make this display when navigating reliably resizing and vertically centering so that the whole line is visible without scrolling and the text size is such as to fill the good portion of the field?
The stack is attached.

Thanks!

keram
Attachments
vertical centering and resizing of text.zip
(20.63 KiB) Downloaded 183 times
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

Post Reply